Bash variable expansion in command [duplicate] - bash

This question already has answers here:
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Difference between single and double quotes in Bash
(7 answers)
Closed 4 years ago.
DATE="1 week ago"
date --date='$DATE'
doesn't work. How can I get it to work?
I could do:
DATE_CMD="date --date='$DATE'"
eval $DATE_CMD
but I don't want to store the entire command in a variable.

You're a victim of quote expansion.
The proper invocation would likely be:
DATE='1 week ago'
date --date="$DATE"
(notice the double quotes)

You just need to use double-quotes to enable string interpolation:
date --date="$date"

Related

bash script, want to use '$' without it looking for a variable [duplicate]

This question already has answers here:
Interpolating variables which contain '$' in a bash script
(1 answer)
Difference between single and double quotes in Bash
(7 answers)
Closed 2 years ago.
I need to write a cron job, to send an mqtt message to "homie/$HOSTNAME/$state".
In the case of $HOSTNAME - it needs to use the env variable, but $state - it must use as is.
How would I add "homie/$HOSTNAME/$state" without bash thinking the $state is a variable as well?

Understand double quote with echo and shebang [duplicate]

This question already has answers here:
echo "#!" fails -- "event not found"
(5 answers)
! in a string makes it unusable in a command line - error message "event not found" [duplicate]
(1 answer)
-bash: !/usr/bin/env: event not found [duplicate]
(1 answer)
Closed 3 years ago.
I have a basic question about Shell interpretation
When i make something like :
echo "#!/bin/bash"
-bash: !/bin/bash: event not found
What does the shell really pass to the echo command.
If i understand well, the double quote make the shell doesnt do globbing.
! is used for history expansion, not for globbing.
From the bash documentation:
Enclosing characters in double quotes preserves the literal value of
all characters within the quotes, with the exception of $, `, \, and,
when history expansion is enabled, !.

Expand bash variable within quotes [duplicate]

This question already has answers here:
Expansion of variables inside single quotes in a command in Bash
(8 answers)
How to echo variable inside single quotes using Bash?
(2 answers)
Closed 5 years ago.
How can I expand APP_VERSION so that it gets used in the rest of my script? As it is right now, it doesn't expand and show the argument I pass in via the CLI.
APP_VERSION=$2
BOOTSTRAP_RUN='[{"Path":"s3://abc/scripts/emr/deploy_zip.sh ${APP_VERSION}","Name":"Custom action"}]'

Inserting Variable into sh Script Command [duplicate]

This question already has answers here:
Filename not printing correctly with underscore "_" in Bash [duplicate]
(2 answers)
Error in string Concatenation in Shell Scripting
(3 answers)
bash variable interpolation separate variables by a hyphen or underscore
(3 answers)
When do we need curly braces around shell variables?
(7 answers)
Closed 5 years ago.
#!/bin/sh -f
set proj_dir="OutputDir"
for projname in lib proj1 proj2
do
mv ./scripts/$projname_BYTECODE ./$proj_dir/scripts/$projname
done
A very simple example of what is not working well for me. $projname_BYTECODE is being interpreted as a variable name but _BYTECODE is actually part of the folder name. Suggestions?
Use ${X} instead of $X, so in your example ${projname}_BYTECODE should do the trick. Have a look at this question for more information: When do we need curly braces in variables using Bash?

In bash, what does dir=${0%/*} means? [duplicate]

This question already has answers here:
What does 'cd ${0%/*}' mean in bash?
(2 answers)
What is the meaning of ${0%/*} in a bash script?
(1 answer)
Closed 6 years ago.
I have found this piece of code while studying a bash script:
dir=${0%/*}
I suspect the code inside the braces to be regex but I don't see what it means. Any idea?
It is not a regex, but it is a pattern match. It sets dir to the name of the script, which is $0, but without the last slash and any non-slash after it, if there is a slash in $0. If there is no slash in $0, dir gets a copy of $0 unchanged. See "Parameter Expansion" in the Bash Hackers Wiki.

Resources