This question already has answers here:
Assignment of variables with space after the (=) sign?
(4 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 24 days ago.
The following command (get process name) works for me
ps -q $$ -o comm=
bash
But once I try to assign to variable, it does not work
processname=ps -q $$ -o comm=
-q: command not found
Why is that?
Related
This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
How to use variables in a command in sed?
(4 answers)
Closed 5 months ago.
when I try to delete particular lines in my file using shellscrpit I use below command, It's not working, So can anyone help me on same.
sed -i '${lineNumber}d' <filename>
This question already has answers here:
How to remove a newline from a string in Bash
(11 answers)
How to remove carriage return from a variable in shell script
(6 answers)
Closed 3 years ago.
file.sh
#!/bin/bash
fileName="Screenshot_$(TZ=GMT-3 date +%Y%m%d_%H%M%S).png"
echo "|$fileName|"
Terminal at Ubuntu 19.04:
> bash file.sh
|Screenshot_20190521_104141.png
|
I want to understand why a new-line is added to the variable at end?
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 ."
This question already has answers here:
Use a variable's value in a sed command [duplicate]
(6 answers)
sed substitution with Bash variables
(6 answers)
Closed 4 years ago.
i am just trying to append a line from my bash script:
sed -i '$ a TEXT' "filename"
but when TEXT is a variable it does not work
sed -i '$ a $VARIABLE' "filename"
i have tested endless iterations with ',''," before/after $ and ${}, escape with \ etc., but i always get a literal $VARIABLE in my file
ps. please do not propose to use echo:)
This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Using a variable to refer to another variable in Bash
(3 answers)
Closed 4 years ago.
Say I have an env variable:
export foo="bar";
declare z="\$foo";
I want to echo the value of "$z", which should be "bar". But if I run:
echo "$z"
I get this literal:
$foo
and if I run this instead:
echo "$(eval "$z")"
I get bar is not a command.
But instead I am looking to just get
"bar"
as the echoed result.
How can I do that?