BASH: Dollar sign substitution after a hash? - bash

I am writing a script that deals with hex color values and I want to substitute in a user provided variable after a hash mark like so:
HEX=$1
COLOR='#$HEX'
But this fails as I believe it is interpreting the hash as a comment? How do I escape the hash so that I can have a variable which contains a string with a hash in it?

That fails because you're using single quotes. There's no variable substitution inside single quotes. Instead, use double quoted:
COLOR="#$HEX"

Single quotes block dollar interpolation. Double ones don't, so this should work:
COLOR="#$HEX"

Related

How to add $USER inside of nested quotes? [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed last year.
I have a variable:
my_var="$('/some/path/to/username/more/path' 'and/something/else')"
and I want to change it to be more generic like:
my_var="$('/some/path/to/${USER}/more/path' 'and/something/else')"
but that doesn't work. How can I get the user inside of there?
The problem is that inside a single quoted string nothing is treated with a special meaning. This makes them good for scripting, eg. grep '^pattern$' ... and sed 's/abc$/def/'.
You can use double quotes instead.
$(...) is a command substitution. Is that correct? If so, then you can nest double qoutes:
my_var="$("/some/path/to/${USER}/more/path" 'and/something/else')"
This should be interpreted as two set of quotes, so the outer double quotes, isn't stopped at the first inner quote:
my_var="$( )" # First set of quotes
"/some/path/to/${USER}/more/path" # Second set of quotes
When assigning a variable, you don't need to wrap the expression in double quotes, so:
my_var=$("/some/path/to/${USER}/more/path" 'and/something/else')
would also be fine in this case. Most other cases you should always wrap parameter expansions ($abc), command substitutions ($(...)) etc. in double quotes as to avoid word splitting and pathname expansions.
However to me it seems like you are trying to create an array instead?
$ my_var=("/some/path/to/${USER}/more/path" 'and/something/else')
$ echo "${my_var[0]}"
/some/path/to/and/more/path

Reading command line parameters inside quotes in a shell function

I want to create a function in zshrc for the following command -
node scripts/node_es6.js scripts/small_run_oneoff.js runMiaEventsStatsJob '{"targetDate": "02-01-2018"}'
I want to pass the targetDate as a command line argument. So, I wrote the following function in zshrc -
function mia-events-stats() {
node scripts/node_es6.js scripts/small_run_oneoff.js runMiaEventsStatsJob '{"targetDate": "$1"}'
}
This however does not work. When I execute mia-events-stats 02-01-2018, the targetDate passed to the actual running code is $1.
What am I missing here?
Characters of a string inside single quotes is quoted. Thus, your dollar sign is read as a normal character.
You should replace your single quotes by doubles quotes to let the magic happen, and escape inner double quotes like that:
"{\"targetDate\": \"$1\"}"
If you need your single quotes to be read, simply add them:
"'{\"targetDate\": \"$1\"}'"
Single quotes won't have any effect thanks to doubles quotes.

using double quotes in bash export statement

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

ruby .split('\n') not splitting on new line

Why does this string not split on each "\n"? (RUBY)
"ADVERTISING [7310]\n\t\tIRS NUMBER:\t\t\t\t061340408\n\t\tSTATE OF INCORPORATION:\t\t\tDE\n\t\tFISCAL YEAR END:\t\t\t0331\n\n\tFILING VALUES:\n\t\tFORM TYPE:\t\t10-Q\n\t\tSEC ACT:\t\t1934 Act\n\t".split('\n')
>> ["ADVERTISING [7310]\n\t\tIRS NUMBER:\t\t\t\t061340408\n\t\tSTATE OF INCORPORATION:\t\t\tDE\n\t\tFISCAL YEAR END:\t\t\t0331\n\n\tFILING VALUES:\n\t\tFORM TYPE:\t\t10-Q\n\t\tSEC ACT:\t\t1934 Act\n\t"]
You need .split("\n"). String interpolation is needed to properly interpret the new line, and double quotes are one way to do that.
In Ruby single quotes around a string means that escape characters are not interpreted. Unlike in C, where single quotes denote a single character. In this case '\n' is actually equivalent to "\\n".
So if you want to split on \n you need to change your code to use double quotes.
.split("\n")
Ruby has the methods String#each_line and String#lines
returns an enum:
http://www.ruby-doc.org/core-1.9.3/String.html#method-i-each_line
returns an array:
http://www.ruby-doc.org/core-2.1.2/String.html#method-i-lines
I didn't test it against your scenario but I bet it will work better than manually choosing the newline chars.
Or a regular expression
.split(/\n/)
You can't use single quotes for this:
"ADVERTISING [7310]\n\t\tIRS NUMBER:\t\t\t\t061340408\n\t\tSTATE OF INCORPORATION:\t\t\tDE\n\t\tFISCAL YEAR END:\t\t\t0331\n\n\tFILING VALUES:\n\t\tFORM TYPE:\t\t10-Q\n\t\tSEC ACT:\t\t1934 Act\n\t".split("\n")

How do I put this string with braces and brackets into a variable in a bash script?

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 ".

Resources