This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Closed 3 years ago.
I have a variable:
rules=L002,L003
This rules variable denotes the files to copy from /usr/lib/vera++/scrpts/rules directory. The extension for the files is .tcl.
I am doing something like:
cp -r /usr/lib/vera++/scripts/rules/{${rules}}.tcl .
What goes wrong is that ${rules} is treated completely as a string. But bash should translate that into:
cp -r /usr/lib/vera++/scripts/rules/{L002,L003}.tcl .
The easiest way to take an argument and make a command from this argument is to use eval command. In your case whole script will look like:
#!/bin/bash
rules=L002,L003
eval "cp -r /usr/lib/vera++/scripts/rules/{${rules}}.tcl ."
Related
This question already has answers here:
How do I deal with a filename that starts with the hyphen (-) character?
(5 answers)
Closed 12 months ago.
I was attempting to use the xxd command in bash, and used the -r argument, but forgot to add the in-file and out-file. I now have a useless file named -r and I cant remove it due to it being an argument for almost any command that I can think of that would be able to delete files.
Is there any other way for me to remove this file?
To remove a file whose name starts with a -, use:
rm -- -r
or
rm ./-r
-- is used to signify the end of command options.
This question already has answers here:
What does "plus colon" ("+:") mean in shell script expressions?
(4 answers)
Closed 5 years ago.
I have a bash script that uses the following syntax:
if [ ! -z ${ARGUMENT+x} ]; then
What is the meaning of the "+x" syntax after the argument name?
It means that if $ARGUMENT is set, it will be replaced by the string x
Let's try in a shell :
$ echo ${ARGUMENT+x}
$ ARGUMENT=123
$ echo ${ARGUMENT+x}
x
You can write this with this form too :
${ARGUMENT:+x}
It have a special meaning with :, it test that variable is empty or unset
Check bash parameter expansion
Rather than discussing the syntax, I'll point out what it is attempting to do: it is trying to deterimine if a variable ARGUMENT is set to any value (empty or non-empty) or not. In bash 4.3 or later, one would use the -v operator instead:
if [[ -v ARGUMENT ]]; then
This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 1 year ago.
Basically, I want to send a variable as $1 in another script without the value it has saved.
#!/bin/bash
echo -e "#!/bin/bash\ncp ~/src/$1" > ~/asset/newfile.sh
So, that in the file newfile.sh it is written:
#!/bin/bash
cp ~/src/$1
You can escape the dollar sign with a backslash:
echo -e "#!/bin/bash\ncp ~/src/\$1"
Or, switch to single quotes:
echo -e '#!/bin/bash\ncp ~/src/$1'
This question already has answers here:
Replace one substring for another string in shell script
(16 answers)
Closed 4 years ago.
I've been trying to wrap my head around this for over an hour now and my searches haven't helped yield the answer.
Trying to set a variable inside a bash script. This variable is taking variable-A and removing variable-B from it.
Prefix="$(echo ${Process} | sed -e 's/${Server}//g')"
So if Process=abcd1wxyz01 and Server=wxyz01, then Prefix should end up being abcd1.
I've tried so many iterations from online searches I honestly can't recall what all I've tried.
Your problem are the quotes, as pointed out in afsal_p's answer.
You could do this with parameter expansion instead:
$ process=abcd1wxyz01
$ server=wxyz01
$ prefix=${process%"$server"}
$ echo "$prefix"
abcd1
The ${word%suffix} expansion removes suffix from the end of word.
please use " instead of ' while using bash variables inside sed:
Prefix="$(echo ${Process} | sed -e "s/${Server}//g")"
echo $Prefix
This question already has answers here:
Can ${var} parameter expansion expressions be nested in bash?
(15 answers)
Closed 4 years ago.
I have the follwing which creates a directory from a list of files:
for file in $(ls *.txt); do
folder=${file//.txt/ };
folder=${folder//./'/'};
folder=${folder//[[:space:]]/};
mkdir -p $folder;
done
Can I link the 3 string manipulation commands that assign folder into one line?
I've tried several things with no success. Is it possible to use the | operator somehow?
Bash cannot do this, but Z Shell (zsh) (which is very similar) can nest the replacements:
for file in *.txt; do
folder=${${${file//.txt/ }//./'/'}//[[:space:]]/};
mkdir -p $folder;
done
(You don't need to do $(ls *.txt) (parsing ls in this way is dangerous: you lose all spaces) since you can just give it *.txt, which properly handles spaces in filenames. This works in any POSIX shell.)