How assign rev command to a variable in shell [duplicate] - shell

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

Related

echo and read with pipe issue [duplicate]

This question already has answers here:
Read values into a shell variable from a pipe
(17 answers)
Why variable values are lost after terminating the loop in bash? [duplicate]
(1 answer)
Closed 17 days ago.
First of all, I am sorry, I am learning the bash and I am a newbie.
Please find the below script.
grep "error" /var/log/syslog | while read line
do
echo $line
done
If I am not wrong,The above script will grep the keyword "error" in /var/log/syslog and will send it inside the while loop as STDIN and output will be displayed.
Also please loop the below script.
echo "hello" | read hi
echo $hi
So when I run this script I am not getting any output, why is that?
should I use any loop? only then I will get output?

Store into var the result of grep command shell script [duplicate]

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?

bash- Assigning a value to a string using a command (rev) [duplicate]

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

Linux bash - extract value from grep output [duplicate]

This question already has answers here:
Get string after character [duplicate]
(5 answers)
Closed 6 years ago.
I have variable $OUTPUT=abc PHYSIN=lalala ghi
How can I extract the value of PHYSIN into another new variable called VETH_NAME,
in other words, I'd like VETH_NAME to be lalala
How can I do so using bash commands?
Thanks
Assuming this is what you are saying :
OUTPUT="abc"
PHYSIN="lalala ghi"
VETH_NAME=$(echo "$PHYSIN" | cut -d" " -f1)
finally :
echo $VETH_NAME
lalala
Use parameter expansion with the %% operator, which drops the longest matching suffix from the expansion.
OUTPUT=abc
PHYSIN="lalala ghi"
VETH_NAME=${PHYSIN%% *}

Unix shell scripting to reverse a string without rev [duplicate]

This question already has answers here:
reverse the order of characters in a string
(14 answers)
Closed 9 years ago.
I want to write a shell script that accepts a string as a command line argument and prints out what is entered in the reverse order with out using the rev command but rather reversing the letters one by one. how would I do that?
so like
if Flower is entered it will print out rewolF
Thanks
A good subject for bash parameter expansion :
#!/bin/bash
for ((i=${#1}; i>=0; i--)); do printf "${1:$i:1}"; done; echo
Example :
./script.sh foobar
Output :
raboof
s1='Flower'
a1=($(echo $s1|fold -w1 ))
for (( i=${#a1[#]}-1;i>=0;i--));do echo "$i=>${a1[i]}"; done
5=>r
4=>e
3=>w
2=>o
1=>l
0=>F

Resources