How do I use a variable with special characters in bash? [duplicate] - linux

This question already has answers here:
How can I escape a double quote inside double quotes?
(9 answers)
Closed 1 year ago.
I have this variable in my bash script:
var="\'?"\"'\"
but when I type echo $var I get nothing. I want echo $var to return "\'?"\"'\". What should I do here?

You can try this
var="'''?\"\"\""
or
var='```?"""'
echo "$var"

Related

How to write result in empty variable? [duplicate]

This question already has answers here:
Assignment of variables with space after the (=) sign?
(4 answers)
Closed 2 years ago.
How to write command result into empty variable?
#!/bin/bash
today=''
$today = $(date)
echo $today
There shouldn't be a space around the =
On variable assignment, no need for the $
#!/bin/bash
today=''
today="$(date)"
echo "${today}"

bash removes consecutive spaces from string [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 2 years ago.
I ran into a strange problem that I do not understand. Why are multiple spaces not present in the output of the following command?
$ d='A B'
$ echo $d
A B
use in double quotes:
echo "$d"

Replace hyphens with underscores in bash script [duplicate]

This question already has answers here:
Replacing some characters in a string with another character
(6 answers)
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
Trying to write a bash script and in one part of it I need to take whatever parameter was passed to it and replace the hyphens with underscores if they exist.
Tried to do the following
#!/usr/bin/env bash
string=$1
string=${string//-/_}
echo $string;
It's telling me that this line string=${string//-/_} fails due to "Bad substitution" but it looks like it should do it? Am I missing something?
There is nothing wrong with your script, and it should work in modern versions of Bash.
But just in case you can simplify that to :
#!/bin/bash
echo "$1" | tr '-' '_'
This is in case that parameter substitution does not work ( which seems to be your case ).
Regards!

Difference between single quotes and double quotes [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
bash string with/without quote and single/double quote [duplicate]
(1 answer)
Closed 5 years ago.
I have two variables declared in shell script
var1=23
var2=27
echo $var1 # Prints 23
echo '$var1' # Prints $var1
echo "$var1" # Prints 23
From the above I have some confusion regarding single and double quotes usage on variables.
please can any one clarify this with an example difference between single and double quote.

Shell script variable value not getting update [duplicate]

This question already has answers here:
What is indirect expansion? What does ${!var*} mean?
(6 answers)
Dynamic variable names in Bash
(19 answers)
Is it possible to build variable names from other variables in bash? [duplicate]
(7 answers)
Closed 5 years ago.
I'm trying to execute below bash shell script, but not getting the expected output. Possible i'm doing something wrong or it's not the way of doing this.
#bin/bash
#set -x
path1_one=/home/dell/scripts
echo $path1_one
param_val=path1_one
param1=$( echo "$param_val" | awk -F '_' '{ print $0 }' )
#path2="$path1"
echo $param1
#echo $path2
Output:
/home/dell/scripts
path1_one
Expected Output:
/home/dell/scripts
/home/dell/scripts
Both variable value should be same,but don't know why param1 value is not reflecting with path1_one
You need to tell the script that you want to use the value of the variable path1, not the name path1.
Use:
path2="$path1"

Resources