Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to assign echo value to a variable in shell

Tags:

shell

unix

Im tring to assign echo value which to a variable but im getting error

Var='(echo $2 | sed -e 's/,/: chararray /g'|  sed -e 's/$/: chararray/')'
echo $var

Input :  sh load.sh file 1,2,3,4
Error load.sh: line 1: chararray: command not found
like image 885
marjun Avatar asked Mar 12 '15 13:03

marjun


People also ask

How do you assign an echo to a variable in Linux?

$ FILES=`sudo find . -type f -print | wc -l` $ echo "There are $FILES in the current working directory." That's it for now, in this article, we explained the methods of assigning the output of a shell command to a variable. You can add your thoughts to this post via the feedback section below.

How do you store output of echo in a variable?

Two commands, `echo` and `who` are used in this example as the nested command. Here, `who` command will execute first that print the user's information of the currently logged in user. The output of the `who` command will execute by `echo` command and the output of `echo` will store into the variable $var.

How do you declare a value to a variable in shell script?

We can declare a variable as a variable name followed by assigning operator (=) and the value, which may be a character or string or a number or a special character. There should not be a space between the assignment operator and the variable name, and the corresponding value.


1 Answers

Var=$(echo "$2" | sed -e 's/,/: chararray /g' | sed -e 's/$/: chararray/')
echo "$Var"

OR

Var=`echo "$2" | sed -e 's/,/: chararray /g' | sed -e 's/$/: chararray/'`
echo "$Var"

Use either $(…) or perhaps `…` backtick notation. However, the backtick notation is deprecated and should be avoided. Also, check the comments by mmgross, Etan Reisner and svlasov to your question. They are all correct.

like image 115
Antxon Avatar answered Oct 09 '22 02:10

Antxon