This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 4 years ago.
I am trying to assign the value of a string to the reversed value of another string (via the 'rev' command). I know the rev command is used like this:
$ echo "hello" | rev
olleh
But what I am trying to do is something like this:
var="hello"
rav=${$var | rev}
I know this isn't correct syntax and it doesn't work, but I was wondering if there was a way to assign a variable using a command, and if so how is it done?
Try this, using command substitution:
var="hello"
rav=$(echo "$var" | rev)
echo $rav
Following may also help you on same.
var="hello"
rav=$(rev <<< "$var")
Output will be as follows:
echo $rav
olleh
Related
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 3 years ago.
I'am creating a shell script to extract a number from a particular line, where one particular string appears (isDone), for that i use a grep, i find the line and can echo it, but i can't store the grep output to a var.
$text var got inumerous tags, one of them having the string "isDone", thats the line i want:
code:
short_str="isDone"
echo "$text" | grep "$short_str"
output:
< s:key name="isDone">1</s:key >
now i want to store that output from grep into a file, and then extract the value (on this case is 1)
what have i tried:
store="$("$text" | grep "$short_str")"
echo "$store"
but that outputs all the file, what am i doing wrong?
This question already has answers here:
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
I'm new to bash script, it is interesting, but somehow I'm struggling with everything.
I have a file, separated by tab "\t" with 2 infos : a string and a number.
I'd like to use both info on each line into a bash script in order to look into a file for those infos.
I'm not even there, I'm struggling to give the arguments from the two columns as two arguments for bash.
#/!bin/bash
FILE="${1}"
while read -r line
do
READ_ID_WH= "echo ${line} | cut -f 1"
POS_HOTSPOT= echo '${line} | cut -f 2'
echo "read id is : ${READ_ID_WH} with position ${POS_HOTSPOT}"
done < ${FILE}
and my file is :
ABCD\t1120
ABC\t1121
I'm launching my command with
./script.sh file_of_data.tsv
What I finally get is :
script.sh: line 8: echo ABCD 1120 | cut -f 1: command not found
I tried a lot of possibilities by browsing SO, and I can't make it to divide my line into two arguments to be used separately in my script :(
Hope you can help me :)
Best,
The quotes cause the shell to look for a command whose name is the string between the quotes.
Apparently you are looking for
while IFS=$'\t' read -r id hotspot; do
echo "read id is: $id with position $hotspot"
done <"$1"
You generally want to avoid capturing things into variables you only use once, but the syntax for that is
id=$(echo "$line" | cut -f1)
See also Correct Bash and shell script variable capitalization and When to wrap quotes around a shell variable?. You can never have whitespace on either side of the = assignment operator (or rather, incorrect whitespace changes the semantics to something you probably don't want).
You have a space after the equals sign on lines 5 and 6, so it thinks you are looking for an executable file named echo ABCD 1120 | cut -f 1 and asking to execute it while assigning the variable READ_ID_WH to the empty string, rather than assigning the string you want to the variable.
This question already has answers here:
How to run script commands from variables?
(3 answers)
Execute command in a variable don't execute the latter part of a pipe
(1 answer)
Running a command that is stored in a variable (including pipes and redirects)
(3 answers)
Closed 5 years ago.
#!/bin/bash
# 1st part
ret=$(ps aux | grep -v grep) # thats OK
echo $ret
# 2nd part
cmd="ps aux | grep -v grep" # a problem with the pipe |
ret=$($cmd)
echo $ret
How can I use a command-string as I have in the 2nd part? Think the pipe is the problem. Tried to escape it but it did not help. Get some snytax error of ps.
Thanks!
You need eval:
ret=$(eval "$cmd")
Using eval is not recommended here. It can lead to unexpected results, especially when variables can be read from untrusted sources (See BashFAQ/048 - Eval command and security issues.
You can solve this in a simple way by defining and calling a function as below
ps_cmd() {
ps aux | grep -v grep
}
and use it in the script as
output="$(ps_cmd)"
echo "$output"
Also a good read would be to see why storing commands in a variable is not a good idea and has a lot of potential pitfalls - BashFAQ/050 - I'm trying to put a command in a variable, but the complex cases always fail!
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 5 years ago.
I'm trying to convert space to underscore in a file name, my script is
like below.
old_file=/home/somedir/otherdir/foobar 20170919.csv
new_file="$(basename "$old_file")" | awk 'gsub(" ","_")'
This script works fine when I use with echo command,
echo "$(basename "$old_file")" | awk 'gsub(" ","_")'
but when it comes to assigning the output to variables, it doesn't work...
Does anybody know the idea?
Actually no need of awk, please note below one replaces all space to underscore, not just filename, it can be path too
$ old_file="/home/somedir/otherdir/foobar 20170919.csv"
$ newfile="${old_file// /_}"
$ echo "$newfile"
/home/somedir/otherdir/foobar_20170919.csv
This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 6 years ago.
I wanted to direct the output of rev command to a variable, I tried different methods and didn't work.
read -p "Enter the number: " n
echo $n | rev
echo "new n is: $p"
I want to assign the output of line 2 to p. How?
Thank you,
To store the output of a command in a variable use a $(...) command substitution:
p=$(echo $n | rev)
For further reference, you can check this link