Nested variables have prevented me from trying to use BASH more extensively... consider the following:
export SYS_DIR='/home/${LOGNAME}/sys'
export APP_DIR='${SYS_DIR}/app'
I always end up with
> set
APP_DIR=/home/${LOGNAME}/sys/app
why? lol
and how do I get what I want =/
I'm not trying to resolve ${${var}}, but rather actual strings as shown above
Use double quotes
export APP_DIR="${SYS_DIR}/app"
Single quotes treat everything inside as literal, not evaluated.
Related
Lets say i have a shell variable like the below with the value :-
myvar="abc"xyz
Now if i print the value of this variable myvar, the output i get is abcxyz
Is there a way to get the exact output that also includes the quotes. I tried several ways to escape double quotes etc but unable to find a way to do it.
use single quotes:
myvar='"abc"xyz'
I am running below shell script
var1="'"
json_variable=$var1{"id":158,"name":"stackoverflow"}$var1
echo $json_variable
I am getting below output
'{id:158,name:stackoverflow}'
How can I get output in below format
'{"id":158,"name":"stackoverflow"}'
Thanks,
I think you are looking for:
json_variable="'{\"id\":158,\"name\":\"stackoverflow\"}'"
or perhaps you want
json_variable="'"'{"id":158,"name":"stackoverflow"}'"'"
or
json_variable=\''{"id":158,"name":"stackoverflow"}'\'
or
read json_variable << \EOF
> '{"id":158,"name":"stackoverflow"}'
> EOF
I'm not sure why you need single quotes specifically in the output, but your root problem is that your assignment never adds the double quotes in the first place. First, quote the double quotes properly, using
json_variable='{"id":158,"name":"stackoverflow"}'
Then, add the single quotes to the echo command:
echo "'$json_variable'"
The same trick can be used to add single quotes to the value without worrying about complicated quoting schemes.
json_variable="'$json_variable'"
I want to define variable in shell script as:
value1 = 40 (this can be number or character)
and want to use as in a text like:
$value1_position.xyz (I basically want 40_position.xyz)
How do I do this?
this should do:
${value1}_position.xyz
beware that the variable should be declared with this syntax
value1=40
notice the absence of spaces around the =
To define a variable, simply make sure there are no spaces between the variable name and value
value1=40
To use that variable in bash substitution, creating what you want, use the $ replacement symbol like so:
${value1}_position.xyz
To append that to your text file
echo "${value1}_position.xyz" >> file.txt
Hello I'm reading a book about bash scripting and the author says to add the following to the end of my .bashrc file. export PATH=~/bin:"$PATH" in order to execute my file from the command line by typing its name. I notice however that if I put export PATH=~/bin:$PATH I can achieve the same result. So my question is what is the difference between the one with quotes and the one without quotes? thanks.
The quotes won't hurt anything, but neither are they necessary. Assignments are processed specially by the shell. From the man page:
A variable may be assigned to by a statement of the form
name=[value]
If value is not given, the variable is assigned the null string. All values undergo tilde expansion, parameter and variable
expansion, command substitution, arithmetic expansion, and
quote removal (see EXPANSION below).
Notice that word-splitting and pathname generation are not on the list in bold. These are the two types of expansion you are trying to prevent by quoting a parameter expansion, but in this context they are not performed. The same rules apply to the assignments that are passed to the export built-in command.
You must include the variable PATH inside double quotes. So that it would handle the filepaths which has spaces but without double quotes, it won't handle the filenames which has spaces in it.
I was facing the same with trying to assign a JSON string to a variable in the terminal.
Wrap it with Single Quotes or Double Quotes
Use single quotes, if you string contains double quotes and vice-versa.
$ export TEMP_ENV='I like the "London" bridge'
$ echo $TEMP_ENV
>> I like the "London" bridge
$ export TEMP_ENV="I like the 'London' bridge"
$ echo $TEMP_ENV
>> I like the 'London' bridge
I am trying to set the following string as a variable in a bash script and am getting some errors. I assume that it is because I need to use quotations or escape it etc.
VARIABLENAME=$([(A"sometest",SomeOtherText "MoreText"),(A"sometext",SomeOtherText 100),(A"Sometext,SomeOtherText "SomeText")]}))
This doesn't work when I try to set it.
The text inside $(...) will be interpreted as a command to run. I believe you want this instead:
VARIABLENAME='[(A"sometest",SomeOtherText "MoreText"),(A"sometext",SomeOtherText 100),(A"Sometext,SomeOtherText "SomeText")]})'
Use single quotes around your string, as it contains double quotes and does not contain any variables to expand.
One error is near the end:
"Sometext,
There is an unclosed ".