This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 7 years ago.
I have a script part where I am checking if the file exist . I dont find where is my mistake because it jumps to else( file exists) and starts downloading that file. Part of code:
...
if [ -f '$ins.img' ];
then
echo '$ins.img already exists'
else
wget http://$2/$ins/$ins.img
fi
...
It's because you're using single quotes in your if. Variables are not filled in inside single quotes, so it will check for a file literally named $ins.img, which obviously doesn't exist.
The solution is easy, use double quotes.
Related
This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 3 months ago.
I was getting errors passing in a command and when I put quotes around that things worked just fine. Just curious how that works.
Double quotes around $# (and similarly, ${array[#]}) prevents globbing and word splitting of individual elements, while still expanding to multiple separate arguments.
See: https://github.com/koalaman/shellcheck/wiki/SC2068
This question already has answers here:
Loop through an array of strings in Bash?
(21 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 3 years ago.
I'm trying to loop through an array in Bash when using rclone. I have set up an array of paths, and I'm looping through them, but I also want to echo the path name in the console window. When I run my code, because the paths have spaces in them, the echo command shows each part of the path, instead of the whole path. Here is my code;
folder[0]="a path here"
folder[1]="another path here"
folder[2]="another path here"
folder[3]="another path here"
for item in ${folder[*]}; do
echo "syncing $item ..."
rclone copy ~/"Documents/$item" Box:"$item" -u -vv --syslog
done
What I see is
syncing 'a' ...
syncing 'path' ...
syncing 'here' ...
syncing 'another' ...
syncing 'path' ...
syncing 'here' ...
What I want to see is the full path string. Not sure what I'm doing wrong here, I guess it's the referencing in the folder[*] causing me the problem.
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 3 years ago.
I'm looking at a bash script with this:
PARAMS="%1;$1;$2;$3;$4;$5;$6;$7;$8;$9"
As I understand it, the parameters that are passed in on script execution will be added to this list.
When I run this:
runscript.sh CONFIG 2>&1
I see this error:
line 76: [[%1;CONFIG;;;;;;;;: command not found
where line 76 contains this:
if [[$PARAMS =~ "CONFIG" ]];
What does the %1 mean and how should I run the script to get it to work?
I didn't have a space after the [[. Once added the script worked.
This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 6 years ago.
It is doing something to my path_bash variable but what?
Google pulls up this but I can't find it exactly.
#!/bin/bash
path_bash="$HOME/root/config/bash/"
source "${path_bash}_private.sh"
source "${path_bash}config.sh"
source "${path_bash}utility.sh"
source "${path_bash}workflow.sh"
source "${path_bash}net.sh"
source "${path_bash}makeHTM.sh"
and can I put
path_bash
in another file?
It's used to tell bash where the name of your variable ends.
And example to explain:
$ a="gg"
$ echo $ab
$ echo ${a}b
ggb
This question already has answers here:
echo "#!" fails -- "event not found"
(5 answers)
Closed 5 years ago.
when i am trying to remove consecutive duplicate lines with
awk "!x[$0]++" file
its reporting x[: Event not found.
even the same case with
sed -i -e "$!N; /^\(.*\)\n\1$/!P;D" file as well reporting
N: Event not found.
i tried with single quotes too, it didn't help
Any idea to fix those
You're invoking the shell's history substitution. Surround the exclamation point with single quotes.