How to set a variable to the output of a command in Bash?
I have a pretty simple script that is something like the following:
#!/bin/bash
VAR1="$1"
MOREF='sudo run command against $VAR1 | grep name | cut -c7-'
echo $MOREF
When I run this script from the command line and pass it the arguments, I am not getting any output. However, when I run the commands contained within the $MOREF
variable, I am able to get output.
I would like to know how one can take the results of a command that needs to be run within a script, save it to a variable, and then output that variable on the screen?
bash shell command-line
add a comment |
I have a pretty simple script that is something like the following:
#!/bin/bash
VAR1="$1"
MOREF='sudo run command against $VAR1 | grep name | cut -c7-'
echo $MOREF
When I run this script from the command line and pass it the arguments, I am not getting any output. However, when I run the commands contained within the $MOREF
variable, I am able to get output.
I would like to know how one can take the results of a command that needs to be run within a script, save it to a variable, and then output that variable on the screen?
bash shell command-line
1
A related question stackoverflow.com/questions/25116521/…
– Sandeepan Nath
Aug 25 '16 at 7:09
15
As an aside, all-caps variables are defined by POSIX for variable names with meaning to the operating system or shell itself, whereas names with at least one lowercase character are reserved for application use. Thus, consider using lowercase names for your own shell variables to avoid unintended conflicts (keeping in mind that setting a shell variable will overwrite any like-named environment variable).
– Charles Duffy
Mar 27 '17 at 15:56
As an aside, capturing output into a variable just so you can thenecho
the variable is a useless use ofecho
, and a useless use of variables.
– tripleee
Jul 21 '18 at 6:58
As a further aside, storing output in variables is often unnecessary. For small, short strings you will need to reference multiple times in your program, this is completely fine, and exactly the way to go; but for processing any nontrivial amounts of data, you want to reshape your process into a pipeline, or use a temporary file.
– tripleee
Jan 18 at 7:56
add a comment |
I have a pretty simple script that is something like the following:
#!/bin/bash
VAR1="$1"
MOREF='sudo run command against $VAR1 | grep name | cut -c7-'
echo $MOREF
When I run this script from the command line and pass it the arguments, I am not getting any output. However, when I run the commands contained within the $MOREF
variable, I am able to get output.
I would like to know how one can take the results of a command that needs to be run within a script, save it to a variable, and then output that variable on the screen?
bash shell command-line
I have a pretty simple script that is something like the following:
#!/bin/bash
VAR1="$1"
MOREF='sudo run command against $VAR1 | grep name | cut -c7-'
echo $MOREF
When I run this script from the command line and pass it the arguments, I am not getting any output. However, when I run the commands contained within the $MOREF
variable, I am able to get output.
I would like to know how one can take the results of a command that needs to be run within a script, save it to a variable, and then output that variable on the screen?
bash shell command-line
bash shell command-line
edited Apr 24 '18 at 2:10
codeforester
18.4k84267
18.4k84267
asked Jan 10 '11 at 20:58
JohnJohn
6,57541823
6,57541823
1
A related question stackoverflow.com/questions/25116521/…
– Sandeepan Nath
Aug 25 '16 at 7:09
15
As an aside, all-caps variables are defined by POSIX for variable names with meaning to the operating system or shell itself, whereas names with at least one lowercase character are reserved for application use. Thus, consider using lowercase names for your own shell variables to avoid unintended conflicts (keeping in mind that setting a shell variable will overwrite any like-named environment variable).
– Charles Duffy
Mar 27 '17 at 15:56
As an aside, capturing output into a variable just so you can thenecho
the variable is a useless use ofecho
, and a useless use of variables.
– tripleee
Jul 21 '18 at 6:58
As a further aside, storing output in variables is often unnecessary. For small, short strings you will need to reference multiple times in your program, this is completely fine, and exactly the way to go; but for processing any nontrivial amounts of data, you want to reshape your process into a pipeline, or use a temporary file.
– tripleee
Jan 18 at 7:56
add a comment |
1
A related question stackoverflow.com/questions/25116521/…
– Sandeepan Nath
Aug 25 '16 at 7:09
15
As an aside, all-caps variables are defined by POSIX for variable names with meaning to the operating system or shell itself, whereas names with at least one lowercase character are reserved for application use. Thus, consider using lowercase names for your own shell variables to avoid unintended conflicts (keeping in mind that setting a shell variable will overwrite any like-named environment variable).
– Charles Duffy
Mar 27 '17 at 15:56
As an aside, capturing output into a variable just so you can thenecho
the variable is a useless use ofecho
, and a useless use of variables.
– tripleee
Jul 21 '18 at 6:58
As a further aside, storing output in variables is often unnecessary. For small, short strings you will need to reference multiple times in your program, this is completely fine, and exactly the way to go; but for processing any nontrivial amounts of data, you want to reshape your process into a pipeline, or use a temporary file.
– tripleee
Jan 18 at 7:56
1
1
A related question stackoverflow.com/questions/25116521/…
– Sandeepan Nath
Aug 25 '16 at 7:09
A related question stackoverflow.com/questions/25116521/…
– Sandeepan Nath
Aug 25 '16 at 7:09
15
15
As an aside, all-caps variables are defined by POSIX for variable names with meaning to the operating system or shell itself, whereas names with at least one lowercase character are reserved for application use. Thus, consider using lowercase names for your own shell variables to avoid unintended conflicts (keeping in mind that setting a shell variable will overwrite any like-named environment variable).
– Charles Duffy
Mar 27 '17 at 15:56
As an aside, all-caps variables are defined by POSIX for variable names with meaning to the operating system or shell itself, whereas names with at least one lowercase character are reserved for application use. Thus, consider using lowercase names for your own shell variables to avoid unintended conflicts (keeping in mind that setting a shell variable will overwrite any like-named environment variable).
– Charles Duffy
Mar 27 '17 at 15:56
As an aside, capturing output into a variable just so you can then
echo
the variable is a useless use of echo
, and a useless use of variables.– tripleee
Jul 21 '18 at 6:58
As an aside, capturing output into a variable just so you can then
echo
the variable is a useless use of echo
, and a useless use of variables.– tripleee
Jul 21 '18 at 6:58
As a further aside, storing output in variables is often unnecessary. For small, short strings you will need to reference multiple times in your program, this is completely fine, and exactly the way to go; but for processing any nontrivial amounts of data, you want to reshape your process into a pipeline, or use a temporary file.
– tripleee
Jan 18 at 7:56
As a further aside, storing output in variables is often unnecessary. For small, short strings you will need to reference multiple times in your program, this is completely fine, and exactly the way to go; but for processing any nontrivial amounts of data, you want to reshape your process into a pipeline, or use a temporary file.
– tripleee
Jan 18 at 7:56
add a comment |
13 Answers
13
active
oldest
votes
In addition to backticks (`command`
), you can use $(command)
, which I find easier to read, and allows for nesting.
OUTPUT="$(ls -1)"
echo "$OUTPUT"
MULTILINE=$(ls
-1)
echo "$MULTILINE"
Quoting ("
) does matter to preserve multi-line values.
54
Can we provide some separator for multi line output ?
– Aryan
Feb 21 '13 at 12:26
61
FYI This is called "command substitution": gnu.org/software/bash/manual/bashref.html#Command-Substitution
– David Doria
Jan 24 '14 at 18:35
13
White space (or lack of whitespace) matters
– Ali
Apr 24 '14 at 10:40
8
@timhc22, the curly braces are irrelevant; it's only the quotes that are important re: whether expansion results are string-split and glob-expanded before being passed to theecho
command.
– Charles Duffy
Apr 21 '15 at 15:37
10
Curly braces can be used when the variable is immediately followed by more characters which could be interpreted as part of the variable name. e.g.$OUTPUTfoo
. They are also required when performing inline string operations on the variable, such as$OUTPUT/foo/bar
– rich remer
Jun 1 '16 at 23:16
|
show 9 more comments
Update (2018): the right way is
$(sudo run command)
You're using the wrong kind of apostrophe. You need `
, not '
. This character is called "backticks" (or "grave accent").
Like this:
#!/bin/bash
VAR1="$1"
VAR2="$2"
MOREF=`sudo run command against "$VAR1" | grep name | cut -c7-`
echo "$MOREF"
25
The backtick syntax is obsolescent, and you really need to put double quotes around the variable interpolation in theecho
.
– tripleee
Dec 28 '15 at 12:28
9
I would add that you have to be careful with the spaces around '=' in the assignment above. You shouln't have any spaces there, otherwise you'll get an incorrect assignment
– Zotov
Jan 5 '16 at 11:07
4
tripleeee's comment is correct. In cygwin (May 2016), `` doesn't work while$()
works. Couldn't fix until I saw this page.
– toddwz
May 13 '16 at 12:42
2
Elaboration such as an example on Update (2018) would be appreciated.
– Eduard
Jul 13 '18 at 13:31
add a comment |
As they have already indicated to you, you should use 'backticks'.
The alternative proposed $(command)
works as well, and it also easier to read,
but note that it is valid only with bash or korn shells (and shells derived from those),
so if your scripts have to be really portable on various Unix systems, you should prefer the old backticks notation.
18
They are overtly cautious. Backticks have been deprecated by POSIX a long time ago; the more modern syntax should be available in most shells from this millennium. (There are still legacy environments coughHP-UXcough which are stuck firmly in the early nineties.)
– tripleee
Sep 18 '14 at 14:40
18
Incorrect.$()
is fully compatible with POSIX sh, as standardized over two decades ago.
– Charles Duffy
Apr 21 '15 at 15:38
2
Note that/bin/sh
on Solaris 10 still does not recognize$(…)
— and AFAIK that's true on Solaris 11 too.
– Jonathan Leffler
Dec 18 '15 at 20:07
1
@JonathanLeffler It is actually no more the case with Solaris 11 where/bin/sh
isksh93
.
– jlliagre
Dec 21 '16 at 17:05
@tripleee - response three years late :-) but I've used$()
in the POSIX shell on HP-UX for the past 10+ years.
– Bob Jarvis
Nov 15 '17 at 3:14
|
show 1 more comment
I know three ways to do:
1) Functions are suitable for such tasks:
func ()
ls -l
Invoke it by saying func
2) Also another suitable solution could be eval:
var="ls -l"
eval $var
3) The third one is using variables directly:
var=$(ls -l)
OR
var=`ls -l`
you can get output of third solution in good way:
echo "$var"
and also in nasty way:
echo $var
1
The first two do not seem to answer the question as it currently stands, and the second is commonly held to be dubious.
– tripleee
Sep 22 '16 at 4:39
1
As someone who is entirely new to bash, why is"$var"
good and$var
nasty?
– Peter
Jan 25 '18 at 7:36
@Peter stackoverflow.com/questions/10067266/…
– tripleee
Jul 21 '18 at 7:10
add a comment |
Some bash tricks I use to set variables from commands
2nd Edit 2018-02-12: Adding a different way, search at bottom of this for long-running tasks!
2018-01-25 Edit: add sample function (for populating vars about disk usage)
First simple old and compatible way
myPi=`echo '4*a(1)' | bc -l`
echo $myPi
3.14159265358979323844
Mostly compatible, second way
As nesting could become heavy, parenthesis was implemented for this
myPi=$(bc -l <<<'4*a(1)')
Nested sample:
SysStarted=$(date -d "$(ps ho lstart 1)" +%s)
echo $SysStarted
1480656334
reading more than one variable (with bashisms)
df -k /
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/dm-0 999320 529020 401488 57% /
If I just want Used value:
array=($(df -k /))
you could see array variable:
declare -p array
declare -a array='([0]="Filesystem" [1]="1K-blocks" [2]="Used" [3]="Available" [
4]="Use%" [5]="Mounted" [6]="on" [7]="/dev/dm-0" [8]="999320" [9]="529020" [10]=
"401488" [11]="57%" [12]="/")'
Then:
echo $array[9]
529020
But I prefer this:
read foo ; read filesystem size used avail prct mountpoint ; < <(df -k /)
echo $used
529020
1st read foo
will just skip header line (variable $foo
will contain something like Filesystem 1K-blocks Used Available Use% Mounted on
)
Sample function for populating some variables:
#!/bin/bash
declare free=0 total=0 used=0
getDiskStat()
local foo
read foo
read foo total used free foo
< <(
df -k $1:-/
)
getDiskStat $1
echo $total $used $free
Nota: declare
line is not required, just for readability.
About sudo cmd | grep ... | cut ...
shell=$(cat /etc/passwd | grep $USER | cut -d : -f 7)
echo $shell
/bin/bash
(Please avoid useless cat
! So this is just 1 fork less:
shell=$(grep $USER </etc/passwd | cut -d : -f 7)
All pipes (|
) implies forks. Where another process have to be run, accessing disk, libraries calls and so on.
So using sed
for sample, will limit subprocess to only one fork:
shell=$(sed </etc/passwd "s/^$USER:.*://p;d")
echo $shell
And with bashisms:
But for many actions, mostly on small files, bash could do the job himself:
while IFS=: read -a line ; do
[ "$line" = "$USER" ] && shell=$line[6]
done </etc/passwd
echo $shell
/bin/bash
or
while IFS=: read loginname encpass uid gid fullname home shell;do
[ "$loginname" = "$USER" ] && break
done </etc/passwd
echo $shell $loginname ...
Going further about variable splitting...
Have a look at my answer to How do I split a string on a delimiter in Bash?
Alternative: reducing forks by using backgrounded long-running tasks
2nd Edit 2018-02-12:
In order to prevent multiple forks like
myPi=$(bc -l <<<'4*a(1)'
myRay=12
myCirc=$(bc -l <<<" 2 * $myPi * $myRay ")
or
myStarted=$(date -d "$(ps ho lstart 1)" +%s)
mySessStart=$(date -d "$(ps ho lstart $$)" +%s)
This work fine, but running many forks is heavy and slow.
and commands like date
and bc
could make many operations, line by line!!
See:
bc -l <<<$'3*4n5*6'
12
30
date -f - +%s < <(ps ho lstart 1 $$)
1516030449
1517853288
So we could use long running background process to make many jobs, without having to initiate a new fork for each request.
We just need some file descriptors and fifos for doing this properly:
mkfifo /tmp/myFifoForBc
exec 5> >(bc -l >/tmp/myFifoForBc)
exec 6</tmp/myFifoForBc
rm /tmp/myFifoForBc
(of course, FD 5
and 6
have to be unused!)... From there, you could use this process by:
echo "3*4" >&5
read -u 6 foo
echo $foo
12
echo >&5 "pi=4*a(1)"
echo >&5 "2*pi*12"
read -u 6 foo
echo $foo
75.39822368615503772256
Into a function newConnector
You may found my newConnector
function on GitHub.Com or on my own site (Nota on github, there is two files, on my site, function and demo are bundled into 1 file which could be sourced for use or just run for demo)
Sample:
. shell_connector.sh
tty
/dev/pts/20
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
29019 pts/20 Ss 0:00 bash
30745 pts/20 R+ 0:00 _ ps --tty pts/20 fw
newConnector /usr/bin/bc "-l" '3*4' 12
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
29019 pts/20 Ss 0:00 bash
30944 pts/20 S 0:00 _ /usr/bin/bc -l
30952 pts/20 R+ 0:00 _ ps --tty pts/20 fw
declare -p PI
bash: declare: PI: not found
myBc '4*a(1)' PI
declare -p PI
declare -- PI="3.14159265358979323844"
The function myBc
let you use the background task with simple syntax, and for date:
newConnector /bin/date '-f - +%s' @0 0
myDate '2000-01-01'
946681200
myDate "$(ps ho lstart 1)" boottime
myDate now now ; read utm idl </proc/uptime
myBc "$now-$boottime" uptime
printf "%sn" $utm%%.* $uptime
42134906
42134906
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
29019 pts/20 Ss 0:00 bash
30944 pts/20 S 0:00 _ /usr/bin/bc -l
32615 pts/20 S 0:00 _ /bin/date -f - +%s
3162 pts/20 R+ 0:00 _ ps --tty pts/20 fw
From there, if you want to end one of background process, you just have to close his fd:
eval "exec $DATEOUT>&-"
eval "exec $DATEIN>&-"
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
4936 pts/20 Ss 0:00 bash
5256 pts/20 S 0:00 _ /usr/bin/bc -l
6358 pts/20 R+ 0:00 _ ps --tty pts/20 fw
which is not needed, because all fd close when main process finish.
The nested sample above is what I was looking for. There may be a simpler way, but what I was looking for was the way to find out if a docker container already exists given its name in an environment variable. So for me:EXISTING_CONTAINER=$(docker ps -a | grep "$(echo $CONTAINER_NAME)")
was the statement I was looking for.
– Capricorn1
Aug 2 '17 at 18:02
1
@capricorn1 That's a useless use ofecho
; you want simplygrep "$CONTAINER_NAME"
– tripleee
Nov 15 '17 at 4:20
2018 Edit: Add sample function (for populating vars about disk usage)
– F. Hauri
Jan 25 '18 at 9:41
add a comment |
Just to be different:
MOREF=$(sudo run command against $VAR1 | grep name | cut -c7-)
add a comment |
If you want to do it with multiline/multiple command/s then you can do this:
output=$( bash <<EOF
#multiline/multiple command/s
EOF
)
Or:
output=$(
#multiline/multiple command/s
)
Example:
#!/bin/bash
output="$( bash <<EOF
echo first
echo second
echo third
EOF
)"
echo "$output"
Output:
first
second
third
Using heredoc you can simplify things pretty easily by breaking down your long single line code into multiline one. Another example:
output="$( ssh -p $port $user@$domain <<EOF
#breakdown your long ssh command into multiline here.
EOF
)"
2
What's with the secondbash
inside the command substitution? You are already creating a subshell by the command substitution itself. If you want to put multiple commands, just separate them by newline or semicolon.output=$(echo first; echo second; ...)
– tripleee
Dec 28 '15 at 12:27
@tripleee just different ways of getting it done.
– Jahid
Dec 29 '15 at 8:43
Then similarly'bash -c "bash -c "bash -c ...""'
would be "different", too; but I don't see the point of that.
– tripleee
Dec 29 '15 at 8:59
1
I don't feel we are communicating properly. I am challenging the usefulness overvariable=$(bash -c 'echo "foo"; echo "bar"')
overvariable=$(echo "foo"; echo "bar")
-- the here document is just a quoting mechanism and doesn't really add anything except another useless complication.
– tripleee
Dec 29 '15 at 9:08
2
When I use heredoc with ssh, I precise the command to runssh -p $port $user@$domain /bin/bash <<EOF
in order to preventPseudo-terminal will not be allocated because stdin is not a terminal.
warning
– F. Hauri
Dec 20 '16 at 7:40
|
show 11 more comments
When setting a variable make sure you have NO Spaces before and/or after the = sign. Literally spent an hour trying to figure this, trying all kinds of solutions! This is Not cool.
Correct:
WTFF=`echo "stuff"`
echo "Example: $WTFF"
Will Fail with error: (stuff: not found or similar)
WTFF= `echo "stuff"`
echo "Example: $WTFF"
I didn't notice this earlier. Thanks :)
– Ayyappa
Sep 26 '17 at 4:07
The version with the space means something different:var=value somecommand
runssomecommand
withvar
in its environment having the valuevalue
. Thus,var= somecommand
is exportingvar
in the environment ofsomecommand
with an empty (zero-byte) value.
– Charles Duffy
Dec 15 '18 at 23:05
add a comment |
You need to use either
$(command-here)
or
`command-here`
example
#!/bin/bash
VAR1="$1"
VAR2="$2"
MOREF="$(sudo run command against "$VAR1" | grep name | cut -c7-)"
echo "$MOREF"
1
$()
is much better than backticks. See: What is the benefit of using $() instead of backticks in shell scripts?
– codeforester
Apr 9 '18 at 18:56
1
I didn't know you could nest but it makes perfect sense, thank you very much for the info!
– Diego Velez
Apr 11 '18 at 2:58
add a comment |
This is another way, good to use with some text editors that are unable to correctly highlight every intricate code you create.
read -r -d '' str < <(cat somefile.txt)
echo "$#str"
echo "$str"
This doesn't deal with OP's question, which is really about command substitution, not process substitution.
– codeforester
Apr 24 '18 at 2:01
@codeforester but did this work for you too?
– Aquarius Power
Apr 29 '18 at 0:54
add a comment |
You can use back-ticks(also known as accent graves) or $()
.
Like as-
OUTPUT=$(x+2);
OUTPUT=`x+2`;
Both have the same effect. But OUTPUT=$(x+2) is more readable and the latest one.
1
bash: x+2: command not found
– F. Hauri
Dec 20 '16 at 7:36
2
Parenthesis was implemented in order to permit nesting.
– F. Hauri
Dec 20 '16 at 7:37
add a comment |
Some may find this useful.
Integer values in variable substitution, where the trick is using $(())
double brackets:
N=3
M=3
COUNT=$N-1
ARR[0]=3
ARR[1]=2
ARR[2]=4
ARR[3]=1
while (( COUNT < $#ARR[@] ))
do
ARR[$COUNT]=$((ARR[COUNT]*M))
(( COUNT=$COUNT+$N ))
done
1
This does not seem to have any relevance for this question. It would be a reasonable answer if somebody were to ask how to multiply a number in an array by a constant factor, though I don't recall ever seeing anyone asking that (and then afor ((...))
loop would seem like a better match for the loop variable). Also, you should not use uppercase for your private variables.
– tripleee
Dec 28 '15 at 12:22
I disagree with the "relevance" part. The question clearly reads: How to set a variable equal to the output from a command in Bash? And I added this answer as a complement because I got here looking for a solution which helped me with the code I later posted. Regarding the uppercase vars, thanks for that.
– Gus
Dec 28 '15 at 13:38
1
So which command's output are you capturing here?
– tripleee
Dec 28 '15 at 15:30
1
This could be writtenARR=(3 2 4 1);for((N=3,M=3,COUNT=N-1;COUNT < $#ARR[@];ARR[COUNT]*=M,COUNT+=N)) :;
but I agree with @tripleee: I don't understand what do this, there!
– F. Hauri
Dec 20 '16 at 7:25
@F.Hauri... bash is getting more & more like perl the deeper you go into it!
– ropata
Nov 7 '17 at 23:22
add a comment |
Here are two more ways:
Please keep in mind that space is very important in bash. So, if you want your command to run, use as is without introducing any more spaces.
following assigns harshil to L and then prints it
L=$"harshil"
echo "$L"following assigns the output of the command
tr
to L2.tr
is being operated on another variable L1.L2=$(echo "$L1" | tr [:upper:] [:lower:])
4
1.$"..."
probably doesn't do what you think it does. 2. This is already given in Andy Lester's answer.
– gniourf_gniourf
Jun 22 '16 at 10:35
@gniourf_gniourf is right: see bash localization won't work with multilines. But under bash, you could useecho $L1,,
to downcase, orecho $L1^^
to upcase.
– F. Hauri
Dec 20 '16 at 7:34
add a comment |
protected by Community♦ Apr 19 '13 at 16:00
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
13 Answers
13
active
oldest
votes
13 Answers
13
active
oldest
votes
active
oldest
votes
active
oldest
votes
In addition to backticks (`command`
), you can use $(command)
, which I find easier to read, and allows for nesting.
OUTPUT="$(ls -1)"
echo "$OUTPUT"
MULTILINE=$(ls
-1)
echo "$MULTILINE"
Quoting ("
) does matter to preserve multi-line values.
54
Can we provide some separator for multi line output ?
– Aryan
Feb 21 '13 at 12:26
61
FYI This is called "command substitution": gnu.org/software/bash/manual/bashref.html#Command-Substitution
– David Doria
Jan 24 '14 at 18:35
13
White space (or lack of whitespace) matters
– Ali
Apr 24 '14 at 10:40
8
@timhc22, the curly braces are irrelevant; it's only the quotes that are important re: whether expansion results are string-split and glob-expanded before being passed to theecho
command.
– Charles Duffy
Apr 21 '15 at 15:37
10
Curly braces can be used when the variable is immediately followed by more characters which could be interpreted as part of the variable name. e.g.$OUTPUTfoo
. They are also required when performing inline string operations on the variable, such as$OUTPUT/foo/bar
– rich remer
Jun 1 '16 at 23:16
|
show 9 more comments
In addition to backticks (`command`
), you can use $(command)
, which I find easier to read, and allows for nesting.
OUTPUT="$(ls -1)"
echo "$OUTPUT"
MULTILINE=$(ls
-1)
echo "$MULTILINE"
Quoting ("
) does matter to preserve multi-line values.
54
Can we provide some separator for multi line output ?
– Aryan
Feb 21 '13 at 12:26
61
FYI This is called "command substitution": gnu.org/software/bash/manual/bashref.html#Command-Substitution
– David Doria
Jan 24 '14 at 18:35
13
White space (or lack of whitespace) matters
– Ali
Apr 24 '14 at 10:40
8
@timhc22, the curly braces are irrelevant; it's only the quotes that are important re: whether expansion results are string-split and glob-expanded before being passed to theecho
command.
– Charles Duffy
Apr 21 '15 at 15:37
10
Curly braces can be used when the variable is immediately followed by more characters which could be interpreted as part of the variable name. e.g.$OUTPUTfoo
. They are also required when performing inline string operations on the variable, such as$OUTPUT/foo/bar
– rich remer
Jun 1 '16 at 23:16
|
show 9 more comments
In addition to backticks (`command`
), you can use $(command)
, which I find easier to read, and allows for nesting.
OUTPUT="$(ls -1)"
echo "$OUTPUT"
MULTILINE=$(ls
-1)
echo "$MULTILINE"
Quoting ("
) does matter to preserve multi-line values.
In addition to backticks (`command`
), you can use $(command)
, which I find easier to read, and allows for nesting.
OUTPUT="$(ls -1)"
echo "$OUTPUT"
MULTILINE=$(ls
-1)
echo "$MULTILINE"
Quoting ("
) does matter to preserve multi-line values.
edited Jan 7 at 14:50
Jonathan
5,29963256
5,29963256
answered Jan 10 '11 at 21:04
Andy LesterAndy Lester
68k1279136
68k1279136
54
Can we provide some separator for multi line output ?
– Aryan
Feb 21 '13 at 12:26
61
FYI This is called "command substitution": gnu.org/software/bash/manual/bashref.html#Command-Substitution
– David Doria
Jan 24 '14 at 18:35
13
White space (or lack of whitespace) matters
– Ali
Apr 24 '14 at 10:40
8
@timhc22, the curly braces are irrelevant; it's only the quotes that are important re: whether expansion results are string-split and glob-expanded before being passed to theecho
command.
– Charles Duffy
Apr 21 '15 at 15:37
10
Curly braces can be used when the variable is immediately followed by more characters which could be interpreted as part of the variable name. e.g.$OUTPUTfoo
. They are also required when performing inline string operations on the variable, such as$OUTPUT/foo/bar
– rich remer
Jun 1 '16 at 23:16
|
show 9 more comments
54
Can we provide some separator for multi line output ?
– Aryan
Feb 21 '13 at 12:26
61
FYI This is called "command substitution": gnu.org/software/bash/manual/bashref.html#Command-Substitution
– David Doria
Jan 24 '14 at 18:35
13
White space (or lack of whitespace) matters
– Ali
Apr 24 '14 at 10:40
8
@timhc22, the curly braces are irrelevant; it's only the quotes that are important re: whether expansion results are string-split and glob-expanded before being passed to theecho
command.
– Charles Duffy
Apr 21 '15 at 15:37
10
Curly braces can be used when the variable is immediately followed by more characters which could be interpreted as part of the variable name. e.g.$OUTPUTfoo
. They are also required when performing inline string operations on the variable, such as$OUTPUT/foo/bar
– rich remer
Jun 1 '16 at 23:16
54
54
Can we provide some separator for multi line output ?
– Aryan
Feb 21 '13 at 12:26
Can we provide some separator for multi line output ?
– Aryan
Feb 21 '13 at 12:26
61
61
FYI This is called "command substitution": gnu.org/software/bash/manual/bashref.html#Command-Substitution
– David Doria
Jan 24 '14 at 18:35
FYI This is called "command substitution": gnu.org/software/bash/manual/bashref.html#Command-Substitution
– David Doria
Jan 24 '14 at 18:35
13
13
White space (or lack of whitespace) matters
– Ali
Apr 24 '14 at 10:40
White space (or lack of whitespace) matters
– Ali
Apr 24 '14 at 10:40
8
8
@timhc22, the curly braces are irrelevant; it's only the quotes that are important re: whether expansion results are string-split and glob-expanded before being passed to the
echo
command.– Charles Duffy
Apr 21 '15 at 15:37
@timhc22, the curly braces are irrelevant; it's only the quotes that are important re: whether expansion results are string-split and glob-expanded before being passed to the
echo
command.– Charles Duffy
Apr 21 '15 at 15:37
10
10
Curly braces can be used when the variable is immediately followed by more characters which could be interpreted as part of the variable name. e.g.
$OUTPUTfoo
. They are also required when performing inline string operations on the variable, such as $OUTPUT/foo/bar
– rich remer
Jun 1 '16 at 23:16
Curly braces can be used when the variable is immediately followed by more characters which could be interpreted as part of the variable name. e.g.
$OUTPUTfoo
. They are also required when performing inline string operations on the variable, such as $OUTPUT/foo/bar
– rich remer
Jun 1 '16 at 23:16
|
show 9 more comments
Update (2018): the right way is
$(sudo run command)
You're using the wrong kind of apostrophe. You need `
, not '
. This character is called "backticks" (or "grave accent").
Like this:
#!/bin/bash
VAR1="$1"
VAR2="$2"
MOREF=`sudo run command against "$VAR1" | grep name | cut -c7-`
echo "$MOREF"
25
The backtick syntax is obsolescent, and you really need to put double quotes around the variable interpolation in theecho
.
– tripleee
Dec 28 '15 at 12:28
9
I would add that you have to be careful with the spaces around '=' in the assignment above. You shouln't have any spaces there, otherwise you'll get an incorrect assignment
– Zotov
Jan 5 '16 at 11:07
4
tripleeee's comment is correct. In cygwin (May 2016), `` doesn't work while$()
works. Couldn't fix until I saw this page.
– toddwz
May 13 '16 at 12:42
2
Elaboration such as an example on Update (2018) would be appreciated.
– Eduard
Jul 13 '18 at 13:31
add a comment |
Update (2018): the right way is
$(sudo run command)
You're using the wrong kind of apostrophe. You need `
, not '
. This character is called "backticks" (or "grave accent").
Like this:
#!/bin/bash
VAR1="$1"
VAR2="$2"
MOREF=`sudo run command against "$VAR1" | grep name | cut -c7-`
echo "$MOREF"
25
The backtick syntax is obsolescent, and you really need to put double quotes around the variable interpolation in theecho
.
– tripleee
Dec 28 '15 at 12:28
9
I would add that you have to be careful with the spaces around '=' in the assignment above. You shouln't have any spaces there, otherwise you'll get an incorrect assignment
– Zotov
Jan 5 '16 at 11:07
4
tripleeee's comment is correct. In cygwin (May 2016), `` doesn't work while$()
works. Couldn't fix until I saw this page.
– toddwz
May 13 '16 at 12:42
2
Elaboration such as an example on Update (2018) would be appreciated.
– Eduard
Jul 13 '18 at 13:31
add a comment |
Update (2018): the right way is
$(sudo run command)
You're using the wrong kind of apostrophe. You need `
, not '
. This character is called "backticks" (or "grave accent").
Like this:
#!/bin/bash
VAR1="$1"
VAR2="$2"
MOREF=`sudo run command against "$VAR1" | grep name | cut -c7-`
echo "$MOREF"
Update (2018): the right way is
$(sudo run command)
You're using the wrong kind of apostrophe. You need `
, not '
. This character is called "backticks" (or "grave accent").
Like this:
#!/bin/bash
VAR1="$1"
VAR2="$2"
MOREF=`sudo run command against "$VAR1" | grep name | cut -c7-`
echo "$MOREF"
edited Apr 29 '18 at 17:08
John Kugelman
245k54406459
245k54406459
answered Jan 10 '11 at 21:00
Ilya KoganIlya Kogan
14.5k1362125
14.5k1362125
25
The backtick syntax is obsolescent, and you really need to put double quotes around the variable interpolation in theecho
.
– tripleee
Dec 28 '15 at 12:28
9
I would add that you have to be careful with the spaces around '=' in the assignment above. You shouln't have any spaces there, otherwise you'll get an incorrect assignment
– Zotov
Jan 5 '16 at 11:07
4
tripleeee's comment is correct. In cygwin (May 2016), `` doesn't work while$()
works. Couldn't fix until I saw this page.
– toddwz
May 13 '16 at 12:42
2
Elaboration such as an example on Update (2018) would be appreciated.
– Eduard
Jul 13 '18 at 13:31
add a comment |
25
The backtick syntax is obsolescent, and you really need to put double quotes around the variable interpolation in theecho
.
– tripleee
Dec 28 '15 at 12:28
9
I would add that you have to be careful with the spaces around '=' in the assignment above. You shouln't have any spaces there, otherwise you'll get an incorrect assignment
– Zotov
Jan 5 '16 at 11:07
4
tripleeee's comment is correct. In cygwin (May 2016), `` doesn't work while$()
works. Couldn't fix until I saw this page.
– toddwz
May 13 '16 at 12:42
2
Elaboration such as an example on Update (2018) would be appreciated.
– Eduard
Jul 13 '18 at 13:31
25
25
The backtick syntax is obsolescent, and you really need to put double quotes around the variable interpolation in the
echo
.– tripleee
Dec 28 '15 at 12:28
The backtick syntax is obsolescent, and you really need to put double quotes around the variable interpolation in the
echo
.– tripleee
Dec 28 '15 at 12:28
9
9
I would add that you have to be careful with the spaces around '=' in the assignment above. You shouln't have any spaces there, otherwise you'll get an incorrect assignment
– Zotov
Jan 5 '16 at 11:07
I would add that you have to be careful with the spaces around '=' in the assignment above. You shouln't have any spaces there, otherwise you'll get an incorrect assignment
– Zotov
Jan 5 '16 at 11:07
4
4
tripleeee's comment is correct. In cygwin (May 2016), `` doesn't work while
$()
works. Couldn't fix until I saw this page.– toddwz
May 13 '16 at 12:42
tripleeee's comment is correct. In cygwin (May 2016), `` doesn't work while
$()
works. Couldn't fix until I saw this page.– toddwz
May 13 '16 at 12:42
2
2
Elaboration such as an example on Update (2018) would be appreciated.
– Eduard
Jul 13 '18 at 13:31
Elaboration such as an example on Update (2018) would be appreciated.
– Eduard
Jul 13 '18 at 13:31
add a comment |
As they have already indicated to you, you should use 'backticks'.
The alternative proposed $(command)
works as well, and it also easier to read,
but note that it is valid only with bash or korn shells (and shells derived from those),
so if your scripts have to be really portable on various Unix systems, you should prefer the old backticks notation.
18
They are overtly cautious. Backticks have been deprecated by POSIX a long time ago; the more modern syntax should be available in most shells from this millennium. (There are still legacy environments coughHP-UXcough which are stuck firmly in the early nineties.)
– tripleee
Sep 18 '14 at 14:40
18
Incorrect.$()
is fully compatible with POSIX sh, as standardized over two decades ago.
– Charles Duffy
Apr 21 '15 at 15:38
2
Note that/bin/sh
on Solaris 10 still does not recognize$(…)
— and AFAIK that's true on Solaris 11 too.
– Jonathan Leffler
Dec 18 '15 at 20:07
1
@JonathanLeffler It is actually no more the case with Solaris 11 where/bin/sh
isksh93
.
– jlliagre
Dec 21 '16 at 17:05
@tripleee - response three years late :-) but I've used$()
in the POSIX shell on HP-UX for the past 10+ years.
– Bob Jarvis
Nov 15 '17 at 3:14
|
show 1 more comment
As they have already indicated to you, you should use 'backticks'.
The alternative proposed $(command)
works as well, and it also easier to read,
but note that it is valid only with bash or korn shells (and shells derived from those),
so if your scripts have to be really portable on various Unix systems, you should prefer the old backticks notation.
18
They are overtly cautious. Backticks have been deprecated by POSIX a long time ago; the more modern syntax should be available in most shells from this millennium. (There are still legacy environments coughHP-UXcough which are stuck firmly in the early nineties.)
– tripleee
Sep 18 '14 at 14:40
18
Incorrect.$()
is fully compatible with POSIX sh, as standardized over two decades ago.
– Charles Duffy
Apr 21 '15 at 15:38
2
Note that/bin/sh
on Solaris 10 still does not recognize$(…)
— and AFAIK that's true on Solaris 11 too.
– Jonathan Leffler
Dec 18 '15 at 20:07
1
@JonathanLeffler It is actually no more the case with Solaris 11 where/bin/sh
isksh93
.
– jlliagre
Dec 21 '16 at 17:05
@tripleee - response three years late :-) but I've used$()
in the POSIX shell on HP-UX for the past 10+ years.
– Bob Jarvis
Nov 15 '17 at 3:14
|
show 1 more comment
As they have already indicated to you, you should use 'backticks'.
The alternative proposed $(command)
works as well, and it also easier to read,
but note that it is valid only with bash or korn shells (and shells derived from those),
so if your scripts have to be really portable on various Unix systems, you should prefer the old backticks notation.
As they have already indicated to you, you should use 'backticks'.
The alternative proposed $(command)
works as well, and it also easier to read,
but note that it is valid only with bash or korn shells (and shells derived from those),
so if your scripts have to be really portable on various Unix systems, you should prefer the old backticks notation.
answered Jan 11 '11 at 22:14
bitwelderbitwelder
83878
83878
18
They are overtly cautious. Backticks have been deprecated by POSIX a long time ago; the more modern syntax should be available in most shells from this millennium. (There are still legacy environments coughHP-UXcough which are stuck firmly in the early nineties.)
– tripleee
Sep 18 '14 at 14:40
18
Incorrect.$()
is fully compatible with POSIX sh, as standardized over two decades ago.
– Charles Duffy
Apr 21 '15 at 15:38
2
Note that/bin/sh
on Solaris 10 still does not recognize$(…)
— and AFAIK that's true on Solaris 11 too.
– Jonathan Leffler
Dec 18 '15 at 20:07
1
@JonathanLeffler It is actually no more the case with Solaris 11 where/bin/sh
isksh93
.
– jlliagre
Dec 21 '16 at 17:05
@tripleee - response three years late :-) but I've used$()
in the POSIX shell on HP-UX for the past 10+ years.
– Bob Jarvis
Nov 15 '17 at 3:14
|
show 1 more comment
18
They are overtly cautious. Backticks have been deprecated by POSIX a long time ago; the more modern syntax should be available in most shells from this millennium. (There are still legacy environments coughHP-UXcough which are stuck firmly in the early nineties.)
– tripleee
Sep 18 '14 at 14:40
18
Incorrect.$()
is fully compatible with POSIX sh, as standardized over two decades ago.
– Charles Duffy
Apr 21 '15 at 15:38
2
Note that/bin/sh
on Solaris 10 still does not recognize$(…)
— and AFAIK that's true on Solaris 11 too.
– Jonathan Leffler
Dec 18 '15 at 20:07
1
@JonathanLeffler It is actually no more the case with Solaris 11 where/bin/sh
isksh93
.
– jlliagre
Dec 21 '16 at 17:05
@tripleee - response three years late :-) but I've used$()
in the POSIX shell on HP-UX for the past 10+ years.
– Bob Jarvis
Nov 15 '17 at 3:14
18
18
They are overtly cautious. Backticks have been deprecated by POSIX a long time ago; the more modern syntax should be available in most shells from this millennium. (There are still legacy environments coughHP-UXcough which are stuck firmly in the early nineties.)
– tripleee
Sep 18 '14 at 14:40
They are overtly cautious. Backticks have been deprecated by POSIX a long time ago; the more modern syntax should be available in most shells from this millennium. (There are still legacy environments coughHP-UXcough which are stuck firmly in the early nineties.)
– tripleee
Sep 18 '14 at 14:40
18
18
Incorrect.
$()
is fully compatible with POSIX sh, as standardized over two decades ago.– Charles Duffy
Apr 21 '15 at 15:38
Incorrect.
$()
is fully compatible with POSIX sh, as standardized over two decades ago.– Charles Duffy
Apr 21 '15 at 15:38
2
2
Note that
/bin/sh
on Solaris 10 still does not recognize $(…)
— and AFAIK that's true on Solaris 11 too.– Jonathan Leffler
Dec 18 '15 at 20:07
Note that
/bin/sh
on Solaris 10 still does not recognize $(…)
— and AFAIK that's true on Solaris 11 too.– Jonathan Leffler
Dec 18 '15 at 20:07
1
1
@JonathanLeffler It is actually no more the case with Solaris 11 where
/bin/sh
is ksh93
.– jlliagre
Dec 21 '16 at 17:05
@JonathanLeffler It is actually no more the case with Solaris 11 where
/bin/sh
is ksh93
.– jlliagre
Dec 21 '16 at 17:05
@tripleee - response three years late :-) but I've used
$()
in the POSIX shell on HP-UX for the past 10+ years.– Bob Jarvis
Nov 15 '17 at 3:14
@tripleee - response three years late :-) but I've used
$()
in the POSIX shell on HP-UX for the past 10+ years.– Bob Jarvis
Nov 15 '17 at 3:14
|
show 1 more comment
I know three ways to do:
1) Functions are suitable for such tasks:
func ()
ls -l
Invoke it by saying func
2) Also another suitable solution could be eval:
var="ls -l"
eval $var
3) The third one is using variables directly:
var=$(ls -l)
OR
var=`ls -l`
you can get output of third solution in good way:
echo "$var"
and also in nasty way:
echo $var
1
The first two do not seem to answer the question as it currently stands, and the second is commonly held to be dubious.
– tripleee
Sep 22 '16 at 4:39
1
As someone who is entirely new to bash, why is"$var"
good and$var
nasty?
– Peter
Jan 25 '18 at 7:36
@Peter stackoverflow.com/questions/10067266/…
– tripleee
Jul 21 '18 at 7:10
add a comment |
I know three ways to do:
1) Functions are suitable for such tasks:
func ()
ls -l
Invoke it by saying func
2) Also another suitable solution could be eval:
var="ls -l"
eval $var
3) The third one is using variables directly:
var=$(ls -l)
OR
var=`ls -l`
you can get output of third solution in good way:
echo "$var"
and also in nasty way:
echo $var
1
The first two do not seem to answer the question as it currently stands, and the second is commonly held to be dubious.
– tripleee
Sep 22 '16 at 4:39
1
As someone who is entirely new to bash, why is"$var"
good and$var
nasty?
– Peter
Jan 25 '18 at 7:36
@Peter stackoverflow.com/questions/10067266/…
– tripleee
Jul 21 '18 at 7:10
add a comment |
I know three ways to do:
1) Functions are suitable for such tasks:
func ()
ls -l
Invoke it by saying func
2) Also another suitable solution could be eval:
var="ls -l"
eval $var
3) The third one is using variables directly:
var=$(ls -l)
OR
var=`ls -l`
you can get output of third solution in good way:
echo "$var"
and also in nasty way:
echo $var
I know three ways to do:
1) Functions are suitable for such tasks:
func ()
ls -l
Invoke it by saying func
2) Also another suitable solution could be eval:
var="ls -l"
eval $var
3) The third one is using variables directly:
var=$(ls -l)
OR
var=`ls -l`
you can get output of third solution in good way:
echo "$var"
and also in nasty way:
echo $var
edited May 17 '14 at 16:22
BoltClock♦
525k12911651202
525k12911651202
answered Feb 13 '14 at 7:31
MLSCMLSC
3,64252871
3,64252871
1
The first two do not seem to answer the question as it currently stands, and the second is commonly held to be dubious.
– tripleee
Sep 22 '16 at 4:39
1
As someone who is entirely new to bash, why is"$var"
good and$var
nasty?
– Peter
Jan 25 '18 at 7:36
@Peter stackoverflow.com/questions/10067266/…
– tripleee
Jul 21 '18 at 7:10
add a comment |
1
The first two do not seem to answer the question as it currently stands, and the second is commonly held to be dubious.
– tripleee
Sep 22 '16 at 4:39
1
As someone who is entirely new to bash, why is"$var"
good and$var
nasty?
– Peter
Jan 25 '18 at 7:36
@Peter stackoverflow.com/questions/10067266/…
– tripleee
Jul 21 '18 at 7:10
1
1
The first two do not seem to answer the question as it currently stands, and the second is commonly held to be dubious.
– tripleee
Sep 22 '16 at 4:39
The first two do not seem to answer the question as it currently stands, and the second is commonly held to be dubious.
– tripleee
Sep 22 '16 at 4:39
1
1
As someone who is entirely new to bash, why is
"$var"
good and $var
nasty?– Peter
Jan 25 '18 at 7:36
As someone who is entirely new to bash, why is
"$var"
good and $var
nasty?– Peter
Jan 25 '18 at 7:36
@Peter stackoverflow.com/questions/10067266/…
– tripleee
Jul 21 '18 at 7:10
@Peter stackoverflow.com/questions/10067266/…
– tripleee
Jul 21 '18 at 7:10
add a comment |
Some bash tricks I use to set variables from commands
2nd Edit 2018-02-12: Adding a different way, search at bottom of this for long-running tasks!
2018-01-25 Edit: add sample function (for populating vars about disk usage)
First simple old and compatible way
myPi=`echo '4*a(1)' | bc -l`
echo $myPi
3.14159265358979323844
Mostly compatible, second way
As nesting could become heavy, parenthesis was implemented for this
myPi=$(bc -l <<<'4*a(1)')
Nested sample:
SysStarted=$(date -d "$(ps ho lstart 1)" +%s)
echo $SysStarted
1480656334
reading more than one variable (with bashisms)
df -k /
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/dm-0 999320 529020 401488 57% /
If I just want Used value:
array=($(df -k /))
you could see array variable:
declare -p array
declare -a array='([0]="Filesystem" [1]="1K-blocks" [2]="Used" [3]="Available" [
4]="Use%" [5]="Mounted" [6]="on" [7]="/dev/dm-0" [8]="999320" [9]="529020" [10]=
"401488" [11]="57%" [12]="/")'
Then:
echo $array[9]
529020
But I prefer this:
read foo ; read filesystem size used avail prct mountpoint ; < <(df -k /)
echo $used
529020
1st read foo
will just skip header line (variable $foo
will contain something like Filesystem 1K-blocks Used Available Use% Mounted on
)
Sample function for populating some variables:
#!/bin/bash
declare free=0 total=0 used=0
getDiskStat()
local foo
read foo
read foo total used free foo
< <(
df -k $1:-/
)
getDiskStat $1
echo $total $used $free
Nota: declare
line is not required, just for readability.
About sudo cmd | grep ... | cut ...
shell=$(cat /etc/passwd | grep $USER | cut -d : -f 7)
echo $shell
/bin/bash
(Please avoid useless cat
! So this is just 1 fork less:
shell=$(grep $USER </etc/passwd | cut -d : -f 7)
All pipes (|
) implies forks. Where another process have to be run, accessing disk, libraries calls and so on.
So using sed
for sample, will limit subprocess to only one fork:
shell=$(sed </etc/passwd "s/^$USER:.*://p;d")
echo $shell
And with bashisms:
But for many actions, mostly on small files, bash could do the job himself:
while IFS=: read -a line ; do
[ "$line" = "$USER" ] && shell=$line[6]
done </etc/passwd
echo $shell
/bin/bash
or
while IFS=: read loginname encpass uid gid fullname home shell;do
[ "$loginname" = "$USER" ] && break
done </etc/passwd
echo $shell $loginname ...
Going further about variable splitting...
Have a look at my answer to How do I split a string on a delimiter in Bash?
Alternative: reducing forks by using backgrounded long-running tasks
2nd Edit 2018-02-12:
In order to prevent multiple forks like
myPi=$(bc -l <<<'4*a(1)'
myRay=12
myCirc=$(bc -l <<<" 2 * $myPi * $myRay ")
or
myStarted=$(date -d "$(ps ho lstart 1)" +%s)
mySessStart=$(date -d "$(ps ho lstart $$)" +%s)
This work fine, but running many forks is heavy and slow.
and commands like date
and bc
could make many operations, line by line!!
See:
bc -l <<<$'3*4n5*6'
12
30
date -f - +%s < <(ps ho lstart 1 $$)
1516030449
1517853288
So we could use long running background process to make many jobs, without having to initiate a new fork for each request.
We just need some file descriptors and fifos for doing this properly:
mkfifo /tmp/myFifoForBc
exec 5> >(bc -l >/tmp/myFifoForBc)
exec 6</tmp/myFifoForBc
rm /tmp/myFifoForBc
(of course, FD 5
and 6
have to be unused!)... From there, you could use this process by:
echo "3*4" >&5
read -u 6 foo
echo $foo
12
echo >&5 "pi=4*a(1)"
echo >&5 "2*pi*12"
read -u 6 foo
echo $foo
75.39822368615503772256
Into a function newConnector
You may found my newConnector
function on GitHub.Com or on my own site (Nota on github, there is two files, on my site, function and demo are bundled into 1 file which could be sourced for use or just run for demo)
Sample:
. shell_connector.sh
tty
/dev/pts/20
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
29019 pts/20 Ss 0:00 bash
30745 pts/20 R+ 0:00 _ ps --tty pts/20 fw
newConnector /usr/bin/bc "-l" '3*4' 12
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
29019 pts/20 Ss 0:00 bash
30944 pts/20 S 0:00 _ /usr/bin/bc -l
30952 pts/20 R+ 0:00 _ ps --tty pts/20 fw
declare -p PI
bash: declare: PI: not found
myBc '4*a(1)' PI
declare -p PI
declare -- PI="3.14159265358979323844"
The function myBc
let you use the background task with simple syntax, and for date:
newConnector /bin/date '-f - +%s' @0 0
myDate '2000-01-01'
946681200
myDate "$(ps ho lstart 1)" boottime
myDate now now ; read utm idl </proc/uptime
myBc "$now-$boottime" uptime
printf "%sn" $utm%%.* $uptime
42134906
42134906
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
29019 pts/20 Ss 0:00 bash
30944 pts/20 S 0:00 _ /usr/bin/bc -l
32615 pts/20 S 0:00 _ /bin/date -f - +%s
3162 pts/20 R+ 0:00 _ ps --tty pts/20 fw
From there, if you want to end one of background process, you just have to close his fd:
eval "exec $DATEOUT>&-"
eval "exec $DATEIN>&-"
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
4936 pts/20 Ss 0:00 bash
5256 pts/20 S 0:00 _ /usr/bin/bc -l
6358 pts/20 R+ 0:00 _ ps --tty pts/20 fw
which is not needed, because all fd close when main process finish.
The nested sample above is what I was looking for. There may be a simpler way, but what I was looking for was the way to find out if a docker container already exists given its name in an environment variable. So for me:EXISTING_CONTAINER=$(docker ps -a | grep "$(echo $CONTAINER_NAME)")
was the statement I was looking for.
– Capricorn1
Aug 2 '17 at 18:02
1
@capricorn1 That's a useless use ofecho
; you want simplygrep "$CONTAINER_NAME"
– tripleee
Nov 15 '17 at 4:20
2018 Edit: Add sample function (for populating vars about disk usage)
– F. Hauri
Jan 25 '18 at 9:41
add a comment |
Some bash tricks I use to set variables from commands
2nd Edit 2018-02-12: Adding a different way, search at bottom of this for long-running tasks!
2018-01-25 Edit: add sample function (for populating vars about disk usage)
First simple old and compatible way
myPi=`echo '4*a(1)' | bc -l`
echo $myPi
3.14159265358979323844
Mostly compatible, second way
As nesting could become heavy, parenthesis was implemented for this
myPi=$(bc -l <<<'4*a(1)')
Nested sample:
SysStarted=$(date -d "$(ps ho lstart 1)" +%s)
echo $SysStarted
1480656334
reading more than one variable (with bashisms)
df -k /
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/dm-0 999320 529020 401488 57% /
If I just want Used value:
array=($(df -k /))
you could see array variable:
declare -p array
declare -a array='([0]="Filesystem" [1]="1K-blocks" [2]="Used" [3]="Available" [
4]="Use%" [5]="Mounted" [6]="on" [7]="/dev/dm-0" [8]="999320" [9]="529020" [10]=
"401488" [11]="57%" [12]="/")'
Then:
echo $array[9]
529020
But I prefer this:
read foo ; read filesystem size used avail prct mountpoint ; < <(df -k /)
echo $used
529020
1st read foo
will just skip header line (variable $foo
will contain something like Filesystem 1K-blocks Used Available Use% Mounted on
)
Sample function for populating some variables:
#!/bin/bash
declare free=0 total=0 used=0
getDiskStat()
local foo
read foo
read foo total used free foo
< <(
df -k $1:-/
)
getDiskStat $1
echo $total $used $free
Nota: declare
line is not required, just for readability.
About sudo cmd | grep ... | cut ...
shell=$(cat /etc/passwd | grep $USER | cut -d : -f 7)
echo $shell
/bin/bash
(Please avoid useless cat
! So this is just 1 fork less:
shell=$(grep $USER </etc/passwd | cut -d : -f 7)
All pipes (|
) implies forks. Where another process have to be run, accessing disk, libraries calls and so on.
So using sed
for sample, will limit subprocess to only one fork:
shell=$(sed </etc/passwd "s/^$USER:.*://p;d")
echo $shell
And with bashisms:
But for many actions, mostly on small files, bash could do the job himself:
while IFS=: read -a line ; do
[ "$line" = "$USER" ] && shell=$line[6]
done </etc/passwd
echo $shell
/bin/bash
or
while IFS=: read loginname encpass uid gid fullname home shell;do
[ "$loginname" = "$USER" ] && break
done </etc/passwd
echo $shell $loginname ...
Going further about variable splitting...
Have a look at my answer to How do I split a string on a delimiter in Bash?
Alternative: reducing forks by using backgrounded long-running tasks
2nd Edit 2018-02-12:
In order to prevent multiple forks like
myPi=$(bc -l <<<'4*a(1)'
myRay=12
myCirc=$(bc -l <<<" 2 * $myPi * $myRay ")
or
myStarted=$(date -d "$(ps ho lstart 1)" +%s)
mySessStart=$(date -d "$(ps ho lstart $$)" +%s)
This work fine, but running many forks is heavy and slow.
and commands like date
and bc
could make many operations, line by line!!
See:
bc -l <<<$'3*4n5*6'
12
30
date -f - +%s < <(ps ho lstart 1 $$)
1516030449
1517853288
So we could use long running background process to make many jobs, without having to initiate a new fork for each request.
We just need some file descriptors and fifos for doing this properly:
mkfifo /tmp/myFifoForBc
exec 5> >(bc -l >/tmp/myFifoForBc)
exec 6</tmp/myFifoForBc
rm /tmp/myFifoForBc
(of course, FD 5
and 6
have to be unused!)... From there, you could use this process by:
echo "3*4" >&5
read -u 6 foo
echo $foo
12
echo >&5 "pi=4*a(1)"
echo >&5 "2*pi*12"
read -u 6 foo
echo $foo
75.39822368615503772256
Into a function newConnector
You may found my newConnector
function on GitHub.Com or on my own site (Nota on github, there is two files, on my site, function and demo are bundled into 1 file which could be sourced for use or just run for demo)
Sample:
. shell_connector.sh
tty
/dev/pts/20
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
29019 pts/20 Ss 0:00 bash
30745 pts/20 R+ 0:00 _ ps --tty pts/20 fw
newConnector /usr/bin/bc "-l" '3*4' 12
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
29019 pts/20 Ss 0:00 bash
30944 pts/20 S 0:00 _ /usr/bin/bc -l
30952 pts/20 R+ 0:00 _ ps --tty pts/20 fw
declare -p PI
bash: declare: PI: not found
myBc '4*a(1)' PI
declare -p PI
declare -- PI="3.14159265358979323844"
The function myBc
let you use the background task with simple syntax, and for date:
newConnector /bin/date '-f - +%s' @0 0
myDate '2000-01-01'
946681200
myDate "$(ps ho lstart 1)" boottime
myDate now now ; read utm idl </proc/uptime
myBc "$now-$boottime" uptime
printf "%sn" $utm%%.* $uptime
42134906
42134906
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
29019 pts/20 Ss 0:00 bash
30944 pts/20 S 0:00 _ /usr/bin/bc -l
32615 pts/20 S 0:00 _ /bin/date -f - +%s
3162 pts/20 R+ 0:00 _ ps --tty pts/20 fw
From there, if you want to end one of background process, you just have to close his fd:
eval "exec $DATEOUT>&-"
eval "exec $DATEIN>&-"
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
4936 pts/20 Ss 0:00 bash
5256 pts/20 S 0:00 _ /usr/bin/bc -l
6358 pts/20 R+ 0:00 _ ps --tty pts/20 fw
which is not needed, because all fd close when main process finish.
The nested sample above is what I was looking for. There may be a simpler way, but what I was looking for was the way to find out if a docker container already exists given its name in an environment variable. So for me:EXISTING_CONTAINER=$(docker ps -a | grep "$(echo $CONTAINER_NAME)")
was the statement I was looking for.
– Capricorn1
Aug 2 '17 at 18:02
1
@capricorn1 That's a useless use ofecho
; you want simplygrep "$CONTAINER_NAME"
– tripleee
Nov 15 '17 at 4:20
2018 Edit: Add sample function (for populating vars about disk usage)
– F. Hauri
Jan 25 '18 at 9:41
add a comment |
Some bash tricks I use to set variables from commands
2nd Edit 2018-02-12: Adding a different way, search at bottom of this for long-running tasks!
2018-01-25 Edit: add sample function (for populating vars about disk usage)
First simple old and compatible way
myPi=`echo '4*a(1)' | bc -l`
echo $myPi
3.14159265358979323844
Mostly compatible, second way
As nesting could become heavy, parenthesis was implemented for this
myPi=$(bc -l <<<'4*a(1)')
Nested sample:
SysStarted=$(date -d "$(ps ho lstart 1)" +%s)
echo $SysStarted
1480656334
reading more than one variable (with bashisms)
df -k /
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/dm-0 999320 529020 401488 57% /
If I just want Used value:
array=($(df -k /))
you could see array variable:
declare -p array
declare -a array='([0]="Filesystem" [1]="1K-blocks" [2]="Used" [3]="Available" [
4]="Use%" [5]="Mounted" [6]="on" [7]="/dev/dm-0" [8]="999320" [9]="529020" [10]=
"401488" [11]="57%" [12]="/")'
Then:
echo $array[9]
529020
But I prefer this:
read foo ; read filesystem size used avail prct mountpoint ; < <(df -k /)
echo $used
529020
1st read foo
will just skip header line (variable $foo
will contain something like Filesystem 1K-blocks Used Available Use% Mounted on
)
Sample function for populating some variables:
#!/bin/bash
declare free=0 total=0 used=0
getDiskStat()
local foo
read foo
read foo total used free foo
< <(
df -k $1:-/
)
getDiskStat $1
echo $total $used $free
Nota: declare
line is not required, just for readability.
About sudo cmd | grep ... | cut ...
shell=$(cat /etc/passwd | grep $USER | cut -d : -f 7)
echo $shell
/bin/bash
(Please avoid useless cat
! So this is just 1 fork less:
shell=$(grep $USER </etc/passwd | cut -d : -f 7)
All pipes (|
) implies forks. Where another process have to be run, accessing disk, libraries calls and so on.
So using sed
for sample, will limit subprocess to only one fork:
shell=$(sed </etc/passwd "s/^$USER:.*://p;d")
echo $shell
And with bashisms:
But for many actions, mostly on small files, bash could do the job himself:
while IFS=: read -a line ; do
[ "$line" = "$USER" ] && shell=$line[6]
done </etc/passwd
echo $shell
/bin/bash
or
while IFS=: read loginname encpass uid gid fullname home shell;do
[ "$loginname" = "$USER" ] && break
done </etc/passwd
echo $shell $loginname ...
Going further about variable splitting...
Have a look at my answer to How do I split a string on a delimiter in Bash?
Alternative: reducing forks by using backgrounded long-running tasks
2nd Edit 2018-02-12:
In order to prevent multiple forks like
myPi=$(bc -l <<<'4*a(1)'
myRay=12
myCirc=$(bc -l <<<" 2 * $myPi * $myRay ")
or
myStarted=$(date -d "$(ps ho lstart 1)" +%s)
mySessStart=$(date -d "$(ps ho lstart $$)" +%s)
This work fine, but running many forks is heavy and slow.
and commands like date
and bc
could make many operations, line by line!!
See:
bc -l <<<$'3*4n5*6'
12
30
date -f - +%s < <(ps ho lstart 1 $$)
1516030449
1517853288
So we could use long running background process to make many jobs, without having to initiate a new fork for each request.
We just need some file descriptors and fifos for doing this properly:
mkfifo /tmp/myFifoForBc
exec 5> >(bc -l >/tmp/myFifoForBc)
exec 6</tmp/myFifoForBc
rm /tmp/myFifoForBc
(of course, FD 5
and 6
have to be unused!)... From there, you could use this process by:
echo "3*4" >&5
read -u 6 foo
echo $foo
12
echo >&5 "pi=4*a(1)"
echo >&5 "2*pi*12"
read -u 6 foo
echo $foo
75.39822368615503772256
Into a function newConnector
You may found my newConnector
function on GitHub.Com or on my own site (Nota on github, there is two files, on my site, function and demo are bundled into 1 file which could be sourced for use or just run for demo)
Sample:
. shell_connector.sh
tty
/dev/pts/20
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
29019 pts/20 Ss 0:00 bash
30745 pts/20 R+ 0:00 _ ps --tty pts/20 fw
newConnector /usr/bin/bc "-l" '3*4' 12
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
29019 pts/20 Ss 0:00 bash
30944 pts/20 S 0:00 _ /usr/bin/bc -l
30952 pts/20 R+ 0:00 _ ps --tty pts/20 fw
declare -p PI
bash: declare: PI: not found
myBc '4*a(1)' PI
declare -p PI
declare -- PI="3.14159265358979323844"
The function myBc
let you use the background task with simple syntax, and for date:
newConnector /bin/date '-f - +%s' @0 0
myDate '2000-01-01'
946681200
myDate "$(ps ho lstart 1)" boottime
myDate now now ; read utm idl </proc/uptime
myBc "$now-$boottime" uptime
printf "%sn" $utm%%.* $uptime
42134906
42134906
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
29019 pts/20 Ss 0:00 bash
30944 pts/20 S 0:00 _ /usr/bin/bc -l
32615 pts/20 S 0:00 _ /bin/date -f - +%s
3162 pts/20 R+ 0:00 _ ps --tty pts/20 fw
From there, if you want to end one of background process, you just have to close his fd:
eval "exec $DATEOUT>&-"
eval "exec $DATEIN>&-"
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
4936 pts/20 Ss 0:00 bash
5256 pts/20 S 0:00 _ /usr/bin/bc -l
6358 pts/20 R+ 0:00 _ ps --tty pts/20 fw
which is not needed, because all fd close when main process finish.
Some bash tricks I use to set variables from commands
2nd Edit 2018-02-12: Adding a different way, search at bottom of this for long-running tasks!
2018-01-25 Edit: add sample function (for populating vars about disk usage)
First simple old and compatible way
myPi=`echo '4*a(1)' | bc -l`
echo $myPi
3.14159265358979323844
Mostly compatible, second way
As nesting could become heavy, parenthesis was implemented for this
myPi=$(bc -l <<<'4*a(1)')
Nested sample:
SysStarted=$(date -d "$(ps ho lstart 1)" +%s)
echo $SysStarted
1480656334
reading more than one variable (with bashisms)
df -k /
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/dm-0 999320 529020 401488 57% /
If I just want Used value:
array=($(df -k /))
you could see array variable:
declare -p array
declare -a array='([0]="Filesystem" [1]="1K-blocks" [2]="Used" [3]="Available" [
4]="Use%" [5]="Mounted" [6]="on" [7]="/dev/dm-0" [8]="999320" [9]="529020" [10]=
"401488" [11]="57%" [12]="/")'
Then:
echo $array[9]
529020
But I prefer this:
read foo ; read filesystem size used avail prct mountpoint ; < <(df -k /)
echo $used
529020
1st read foo
will just skip header line (variable $foo
will contain something like Filesystem 1K-blocks Used Available Use% Mounted on
)
Sample function for populating some variables:
#!/bin/bash
declare free=0 total=0 used=0
getDiskStat()
local foo
read foo
read foo total used free foo
< <(
df -k $1:-/
)
getDiskStat $1
echo $total $used $free
Nota: declare
line is not required, just for readability.
About sudo cmd | grep ... | cut ...
shell=$(cat /etc/passwd | grep $USER | cut -d : -f 7)
echo $shell
/bin/bash
(Please avoid useless cat
! So this is just 1 fork less:
shell=$(grep $USER </etc/passwd | cut -d : -f 7)
All pipes (|
) implies forks. Where another process have to be run, accessing disk, libraries calls and so on.
So using sed
for sample, will limit subprocess to only one fork:
shell=$(sed </etc/passwd "s/^$USER:.*://p;d")
echo $shell
And with bashisms:
But for many actions, mostly on small files, bash could do the job himself:
while IFS=: read -a line ; do
[ "$line" = "$USER" ] && shell=$line[6]
done </etc/passwd
echo $shell
/bin/bash
or
while IFS=: read loginname encpass uid gid fullname home shell;do
[ "$loginname" = "$USER" ] && break
done </etc/passwd
echo $shell $loginname ...
Going further about variable splitting...
Have a look at my answer to How do I split a string on a delimiter in Bash?
Alternative: reducing forks by using backgrounded long-running tasks
2nd Edit 2018-02-12:
In order to prevent multiple forks like
myPi=$(bc -l <<<'4*a(1)'
myRay=12
myCirc=$(bc -l <<<" 2 * $myPi * $myRay ")
or
myStarted=$(date -d "$(ps ho lstart 1)" +%s)
mySessStart=$(date -d "$(ps ho lstart $$)" +%s)
This work fine, but running many forks is heavy and slow.
and commands like date
and bc
could make many operations, line by line!!
See:
bc -l <<<$'3*4n5*6'
12
30
date -f - +%s < <(ps ho lstart 1 $$)
1516030449
1517853288
So we could use long running background process to make many jobs, without having to initiate a new fork for each request.
We just need some file descriptors and fifos for doing this properly:
mkfifo /tmp/myFifoForBc
exec 5> >(bc -l >/tmp/myFifoForBc)
exec 6</tmp/myFifoForBc
rm /tmp/myFifoForBc
(of course, FD 5
and 6
have to be unused!)... From there, you could use this process by:
echo "3*4" >&5
read -u 6 foo
echo $foo
12
echo >&5 "pi=4*a(1)"
echo >&5 "2*pi*12"
read -u 6 foo
echo $foo
75.39822368615503772256
Into a function newConnector
You may found my newConnector
function on GitHub.Com or on my own site (Nota on github, there is two files, on my site, function and demo are bundled into 1 file which could be sourced for use or just run for demo)
Sample:
. shell_connector.sh
tty
/dev/pts/20
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
29019 pts/20 Ss 0:00 bash
30745 pts/20 R+ 0:00 _ ps --tty pts/20 fw
newConnector /usr/bin/bc "-l" '3*4' 12
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
29019 pts/20 Ss 0:00 bash
30944 pts/20 S 0:00 _ /usr/bin/bc -l
30952 pts/20 R+ 0:00 _ ps --tty pts/20 fw
declare -p PI
bash: declare: PI: not found
myBc '4*a(1)' PI
declare -p PI
declare -- PI="3.14159265358979323844"
The function myBc
let you use the background task with simple syntax, and for date:
newConnector /bin/date '-f - +%s' @0 0
myDate '2000-01-01'
946681200
myDate "$(ps ho lstart 1)" boottime
myDate now now ; read utm idl </proc/uptime
myBc "$now-$boottime" uptime
printf "%sn" $utm%%.* $uptime
42134906
42134906
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
29019 pts/20 Ss 0:00 bash
30944 pts/20 S 0:00 _ /usr/bin/bc -l
32615 pts/20 S 0:00 _ /bin/date -f - +%s
3162 pts/20 R+ 0:00 _ ps --tty pts/20 fw
From there, if you want to end one of background process, you just have to close his fd:
eval "exec $DATEOUT>&-"
eval "exec $DATEIN>&-"
ps --tty pts/20 fw
PID TTY STAT TIME COMMAND
4936 pts/20 Ss 0:00 bash
5256 pts/20 S 0:00 _ /usr/bin/bc -l
6358 pts/20 R+ 0:00 _ ps --tty pts/20 fw
which is not needed, because all fd close when main process finish.
edited Feb 19 at 13:59
answered Dec 20 '16 at 7:06
F. HauriF. Hauri
36.6k86791
36.6k86791
The nested sample above is what I was looking for. There may be a simpler way, but what I was looking for was the way to find out if a docker container already exists given its name in an environment variable. So for me:EXISTING_CONTAINER=$(docker ps -a | grep "$(echo $CONTAINER_NAME)")
was the statement I was looking for.
– Capricorn1
Aug 2 '17 at 18:02
1
@capricorn1 That's a useless use ofecho
; you want simplygrep "$CONTAINER_NAME"
– tripleee
Nov 15 '17 at 4:20
2018 Edit: Add sample function (for populating vars about disk usage)
– F. Hauri
Jan 25 '18 at 9:41
add a comment |
The nested sample above is what I was looking for. There may be a simpler way, but what I was looking for was the way to find out if a docker container already exists given its name in an environment variable. So for me:EXISTING_CONTAINER=$(docker ps -a | grep "$(echo $CONTAINER_NAME)")
was the statement I was looking for.
– Capricorn1
Aug 2 '17 at 18:02
1
@capricorn1 That's a useless use ofecho
; you want simplygrep "$CONTAINER_NAME"
– tripleee
Nov 15 '17 at 4:20
2018 Edit: Add sample function (for populating vars about disk usage)
– F. Hauri
Jan 25 '18 at 9:41
The nested sample above is what I was looking for. There may be a simpler way, but what I was looking for was the way to find out if a docker container already exists given its name in an environment variable. So for me:
EXISTING_CONTAINER=$(docker ps -a | grep "$(echo $CONTAINER_NAME)")
was the statement I was looking for.– Capricorn1
Aug 2 '17 at 18:02
The nested sample above is what I was looking for. There may be a simpler way, but what I was looking for was the way to find out if a docker container already exists given its name in an environment variable. So for me:
EXISTING_CONTAINER=$(docker ps -a | grep "$(echo $CONTAINER_NAME)")
was the statement I was looking for.– Capricorn1
Aug 2 '17 at 18:02
1
1
@capricorn1 That's a useless use of
echo
; you want simply grep "$CONTAINER_NAME"
– tripleee
Nov 15 '17 at 4:20
@capricorn1 That's a useless use of
echo
; you want simply grep "$CONTAINER_NAME"
– tripleee
Nov 15 '17 at 4:20
2018 Edit: Add sample function (for populating vars about disk usage)
– F. Hauri
Jan 25 '18 at 9:41
2018 Edit: Add sample function (for populating vars about disk usage)
– F. Hauri
Jan 25 '18 at 9:41
add a comment |
Just to be different:
MOREF=$(sudo run command against $VAR1 | grep name | cut -c7-)
add a comment |
Just to be different:
MOREF=$(sudo run command against $VAR1 | grep name | cut -c7-)
add a comment |
Just to be different:
MOREF=$(sudo run command against $VAR1 | grep name | cut -c7-)
Just to be different:
MOREF=$(sudo run command against $VAR1 | grep name | cut -c7-)
answered Jan 10 '11 at 21:07
DigitalRossDigitalRoss
123k18209297
123k18209297
add a comment |
add a comment |
If you want to do it with multiline/multiple command/s then you can do this:
output=$( bash <<EOF
#multiline/multiple command/s
EOF
)
Or:
output=$(
#multiline/multiple command/s
)
Example:
#!/bin/bash
output="$( bash <<EOF
echo first
echo second
echo third
EOF
)"
echo "$output"
Output:
first
second
third
Using heredoc you can simplify things pretty easily by breaking down your long single line code into multiline one. Another example:
output="$( ssh -p $port $user@$domain <<EOF
#breakdown your long ssh command into multiline here.
EOF
)"
2
What's with the secondbash
inside the command substitution? You are already creating a subshell by the command substitution itself. If you want to put multiple commands, just separate them by newline or semicolon.output=$(echo first; echo second; ...)
– tripleee
Dec 28 '15 at 12:27
@tripleee just different ways of getting it done.
– Jahid
Dec 29 '15 at 8:43
Then similarly'bash -c "bash -c "bash -c ...""'
would be "different", too; but I don't see the point of that.
– tripleee
Dec 29 '15 at 8:59
1
I don't feel we are communicating properly. I am challenging the usefulness overvariable=$(bash -c 'echo "foo"; echo "bar"')
overvariable=$(echo "foo"; echo "bar")
-- the here document is just a quoting mechanism and doesn't really add anything except another useless complication.
– tripleee
Dec 29 '15 at 9:08
2
When I use heredoc with ssh, I precise the command to runssh -p $port $user@$domain /bin/bash <<EOF
in order to preventPseudo-terminal will not be allocated because stdin is not a terminal.
warning
– F. Hauri
Dec 20 '16 at 7:40
|
show 11 more comments
If you want to do it with multiline/multiple command/s then you can do this:
output=$( bash <<EOF
#multiline/multiple command/s
EOF
)
Or:
output=$(
#multiline/multiple command/s
)
Example:
#!/bin/bash
output="$( bash <<EOF
echo first
echo second
echo third
EOF
)"
echo "$output"
Output:
first
second
third
Using heredoc you can simplify things pretty easily by breaking down your long single line code into multiline one. Another example:
output="$( ssh -p $port $user@$domain <<EOF
#breakdown your long ssh command into multiline here.
EOF
)"
2
What's with the secondbash
inside the command substitution? You are already creating a subshell by the command substitution itself. If you want to put multiple commands, just separate them by newline or semicolon.output=$(echo first; echo second; ...)
– tripleee
Dec 28 '15 at 12:27
@tripleee just different ways of getting it done.
– Jahid
Dec 29 '15 at 8:43
Then similarly'bash -c "bash -c "bash -c ...""'
would be "different", too; but I don't see the point of that.
– tripleee
Dec 29 '15 at 8:59
1
I don't feel we are communicating properly. I am challenging the usefulness overvariable=$(bash -c 'echo "foo"; echo "bar"')
overvariable=$(echo "foo"; echo "bar")
-- the here document is just a quoting mechanism and doesn't really add anything except another useless complication.
– tripleee
Dec 29 '15 at 9:08
2
When I use heredoc with ssh, I precise the command to runssh -p $port $user@$domain /bin/bash <<EOF
in order to preventPseudo-terminal will not be allocated because stdin is not a terminal.
warning
– F. Hauri
Dec 20 '16 at 7:40
|
show 11 more comments
If you want to do it with multiline/multiple command/s then you can do this:
output=$( bash <<EOF
#multiline/multiple command/s
EOF
)
Or:
output=$(
#multiline/multiple command/s
)
Example:
#!/bin/bash
output="$( bash <<EOF
echo first
echo second
echo third
EOF
)"
echo "$output"
Output:
first
second
third
Using heredoc you can simplify things pretty easily by breaking down your long single line code into multiline one. Another example:
output="$( ssh -p $port $user@$domain <<EOF
#breakdown your long ssh command into multiline here.
EOF
)"
If you want to do it with multiline/multiple command/s then you can do this:
output=$( bash <<EOF
#multiline/multiple command/s
EOF
)
Or:
output=$(
#multiline/multiple command/s
)
Example:
#!/bin/bash
output="$( bash <<EOF
echo first
echo second
echo third
EOF
)"
echo "$output"
Output:
first
second
third
Using heredoc you can simplify things pretty easily by breaking down your long single line code into multiline one. Another example:
output="$( ssh -p $port $user@$domain <<EOF
#breakdown your long ssh command into multiline here.
EOF
)"
edited Dec 29 '15 at 11:16
answered Jun 1 '15 at 17:38
JahidJahid
13.8k46084
13.8k46084
2
What's with the secondbash
inside the command substitution? You are already creating a subshell by the command substitution itself. If you want to put multiple commands, just separate them by newline or semicolon.output=$(echo first; echo second; ...)
– tripleee
Dec 28 '15 at 12:27
@tripleee just different ways of getting it done.
– Jahid
Dec 29 '15 at 8:43
Then similarly'bash -c "bash -c "bash -c ...""'
would be "different", too; but I don't see the point of that.
– tripleee
Dec 29 '15 at 8:59
1
I don't feel we are communicating properly. I am challenging the usefulness overvariable=$(bash -c 'echo "foo"; echo "bar"')
overvariable=$(echo "foo"; echo "bar")
-- the here document is just a quoting mechanism and doesn't really add anything except another useless complication.
– tripleee
Dec 29 '15 at 9:08
2
When I use heredoc with ssh, I precise the command to runssh -p $port $user@$domain /bin/bash <<EOF
in order to preventPseudo-terminal will not be allocated because stdin is not a terminal.
warning
– F. Hauri
Dec 20 '16 at 7:40
|
show 11 more comments
2
What's with the secondbash
inside the command substitution? You are already creating a subshell by the command substitution itself. If you want to put multiple commands, just separate them by newline or semicolon.output=$(echo first; echo second; ...)
– tripleee
Dec 28 '15 at 12:27
@tripleee just different ways of getting it done.
– Jahid
Dec 29 '15 at 8:43
Then similarly'bash -c "bash -c "bash -c ...""'
would be "different", too; but I don't see the point of that.
– tripleee
Dec 29 '15 at 8:59
1
I don't feel we are communicating properly. I am challenging the usefulness overvariable=$(bash -c 'echo "foo"; echo "bar"')
overvariable=$(echo "foo"; echo "bar")
-- the here document is just a quoting mechanism and doesn't really add anything except another useless complication.
– tripleee
Dec 29 '15 at 9:08
2
When I use heredoc with ssh, I precise the command to runssh -p $port $user@$domain /bin/bash <<EOF
in order to preventPseudo-terminal will not be allocated because stdin is not a terminal.
warning
– F. Hauri
Dec 20 '16 at 7:40
2
2
What's with the second
bash
inside the command substitution? You are already creating a subshell by the command substitution itself. If you want to put multiple commands, just separate them by newline or semicolon. output=$(echo first; echo second; ...)
– tripleee
Dec 28 '15 at 12:27
What's with the second
bash
inside the command substitution? You are already creating a subshell by the command substitution itself. If you want to put multiple commands, just separate them by newline or semicolon. output=$(echo first; echo second; ...)
– tripleee
Dec 28 '15 at 12:27
@tripleee just different ways of getting it done.
– Jahid
Dec 29 '15 at 8:43
@tripleee just different ways of getting it done.
– Jahid
Dec 29 '15 at 8:43
Then similarly
'bash -c "bash -c "bash -c ...""'
would be "different", too; but I don't see the point of that.– tripleee
Dec 29 '15 at 8:59
Then similarly
'bash -c "bash -c "bash -c ...""'
would be "different", too; but I don't see the point of that.– tripleee
Dec 29 '15 at 8:59
1
1
I don't feel we are communicating properly. I am challenging the usefulness over
variable=$(bash -c 'echo "foo"; echo "bar"')
over variable=$(echo "foo"; echo "bar")
-- the here document is just a quoting mechanism and doesn't really add anything except another useless complication.– tripleee
Dec 29 '15 at 9:08
I don't feel we are communicating properly. I am challenging the usefulness over
variable=$(bash -c 'echo "foo"; echo "bar"')
over variable=$(echo "foo"; echo "bar")
-- the here document is just a quoting mechanism and doesn't really add anything except another useless complication.– tripleee
Dec 29 '15 at 9:08
2
2
When I use heredoc with ssh, I precise the command to run
ssh -p $port $user@$domain /bin/bash <<EOF
in order to prevent Pseudo-terminal will not be allocated because stdin is not a terminal.
warning– F. Hauri
Dec 20 '16 at 7:40
When I use heredoc with ssh, I precise the command to run
ssh -p $port $user@$domain /bin/bash <<EOF
in order to prevent Pseudo-terminal will not be allocated because stdin is not a terminal.
warning– F. Hauri
Dec 20 '16 at 7:40
|
show 11 more comments
When setting a variable make sure you have NO Spaces before and/or after the = sign. Literally spent an hour trying to figure this, trying all kinds of solutions! This is Not cool.
Correct:
WTFF=`echo "stuff"`
echo "Example: $WTFF"
Will Fail with error: (stuff: not found or similar)
WTFF= `echo "stuff"`
echo "Example: $WTFF"
I didn't notice this earlier. Thanks :)
– Ayyappa
Sep 26 '17 at 4:07
The version with the space means something different:var=value somecommand
runssomecommand
withvar
in its environment having the valuevalue
. Thus,var= somecommand
is exportingvar
in the environment ofsomecommand
with an empty (zero-byte) value.
– Charles Duffy
Dec 15 '18 at 23:05
add a comment |
When setting a variable make sure you have NO Spaces before and/or after the = sign. Literally spent an hour trying to figure this, trying all kinds of solutions! This is Not cool.
Correct:
WTFF=`echo "stuff"`
echo "Example: $WTFF"
Will Fail with error: (stuff: not found or similar)
WTFF= `echo "stuff"`
echo "Example: $WTFF"
I didn't notice this earlier. Thanks :)
– Ayyappa
Sep 26 '17 at 4:07
The version with the space means something different:var=value somecommand
runssomecommand
withvar
in its environment having the valuevalue
. Thus,var= somecommand
is exportingvar
in the environment ofsomecommand
with an empty (zero-byte) value.
– Charles Duffy
Dec 15 '18 at 23:05
add a comment |
When setting a variable make sure you have NO Spaces before and/or after the = sign. Literally spent an hour trying to figure this, trying all kinds of solutions! This is Not cool.
Correct:
WTFF=`echo "stuff"`
echo "Example: $WTFF"
Will Fail with error: (stuff: not found or similar)
WTFF= `echo "stuff"`
echo "Example: $WTFF"
When setting a variable make sure you have NO Spaces before and/or after the = sign. Literally spent an hour trying to figure this, trying all kinds of solutions! This is Not cool.
Correct:
WTFF=`echo "stuff"`
echo "Example: $WTFF"
Will Fail with error: (stuff: not found or similar)
WTFF= `echo "stuff"`
echo "Example: $WTFF"
answered Jul 18 '17 at 11:42
EmilEmil
30134
30134
I didn't notice this earlier. Thanks :)
– Ayyappa
Sep 26 '17 at 4:07
The version with the space means something different:var=value somecommand
runssomecommand
withvar
in its environment having the valuevalue
. Thus,var= somecommand
is exportingvar
in the environment ofsomecommand
with an empty (zero-byte) value.
– Charles Duffy
Dec 15 '18 at 23:05
add a comment |
I didn't notice this earlier. Thanks :)
– Ayyappa
Sep 26 '17 at 4:07
The version with the space means something different:var=value somecommand
runssomecommand
withvar
in its environment having the valuevalue
. Thus,var= somecommand
is exportingvar
in the environment ofsomecommand
with an empty (zero-byte) value.
– Charles Duffy
Dec 15 '18 at 23:05
I didn't notice this earlier. Thanks :)
– Ayyappa
Sep 26 '17 at 4:07
I didn't notice this earlier. Thanks :)
– Ayyappa
Sep 26 '17 at 4:07
The version with the space means something different:
var=value somecommand
runs somecommand
with var
in its environment having the value value
. Thus, var= somecommand
is exporting var
in the environment of somecommand
with an empty (zero-byte) value.– Charles Duffy
Dec 15 '18 at 23:05
The version with the space means something different:
var=value somecommand
runs somecommand
with var
in its environment having the value value
. Thus, var= somecommand
is exporting var
in the environment of somecommand
with an empty (zero-byte) value.– Charles Duffy
Dec 15 '18 at 23:05
add a comment |
You need to use either
$(command-here)
or
`command-here`
example
#!/bin/bash
VAR1="$1"
VAR2="$2"
MOREF="$(sudo run command against "$VAR1" | grep name | cut -c7-)"
echo "$MOREF"
1
$()
is much better than backticks. See: What is the benefit of using $() instead of backticks in shell scripts?
– codeforester
Apr 9 '18 at 18:56
1
I didn't know you could nest but it makes perfect sense, thank you very much for the info!
– Diego Velez
Apr 11 '18 at 2:58
add a comment |
You need to use either
$(command-here)
or
`command-here`
example
#!/bin/bash
VAR1="$1"
VAR2="$2"
MOREF="$(sudo run command against "$VAR1" | grep name | cut -c7-)"
echo "$MOREF"
1
$()
is much better than backticks. See: What is the benefit of using $() instead of backticks in shell scripts?
– codeforester
Apr 9 '18 at 18:56
1
I didn't know you could nest but it makes perfect sense, thank you very much for the info!
– Diego Velez
Apr 11 '18 at 2:58
add a comment |
You need to use either
$(command-here)
or
`command-here`
example
#!/bin/bash
VAR1="$1"
VAR2="$2"
MOREF="$(sudo run command against "$VAR1" | grep name | cut -c7-)"
echo "$MOREF"
You need to use either
$(command-here)
or
`command-here`
example
#!/bin/bash
VAR1="$1"
VAR2="$2"
MOREF="$(sudo run command against "$VAR1" | grep name | cut -c7-)"
echo "$MOREF"
edited Jul 21 '18 at 7:01
tripleee
93.7k13130185
93.7k13130185
answered Dec 28 '17 at 23:44
Diego VelezDiego Velez
412412
412412
1
$()
is much better than backticks. See: What is the benefit of using $() instead of backticks in shell scripts?
– codeforester
Apr 9 '18 at 18:56
1
I didn't know you could nest but it makes perfect sense, thank you very much for the info!
– Diego Velez
Apr 11 '18 at 2:58
add a comment |
1
$()
is much better than backticks. See: What is the benefit of using $() instead of backticks in shell scripts?
– codeforester
Apr 9 '18 at 18:56
1
I didn't know you could nest but it makes perfect sense, thank you very much for the info!
– Diego Velez
Apr 11 '18 at 2:58
1
1
$()
is much better than backticks. See: What is the benefit of using $() instead of backticks in shell scripts?– codeforester
Apr 9 '18 at 18:56
$()
is much better than backticks. See: What is the benefit of using $() instead of backticks in shell scripts?– codeforester
Apr 9 '18 at 18:56
1
1
I didn't know you could nest but it makes perfect sense, thank you very much for the info!
– Diego Velez
Apr 11 '18 at 2:58
I didn't know you could nest but it makes perfect sense, thank you very much for the info!
– Diego Velez
Apr 11 '18 at 2:58
add a comment |
This is another way, good to use with some text editors that are unable to correctly highlight every intricate code you create.
read -r -d '' str < <(cat somefile.txt)
echo "$#str"
echo "$str"
This doesn't deal with OP's question, which is really about command substitution, not process substitution.
– codeforester
Apr 24 '18 at 2:01
@codeforester but did this work for you too?
– Aquarius Power
Apr 29 '18 at 0:54
add a comment |
This is another way, good to use with some text editors that are unable to correctly highlight every intricate code you create.
read -r -d '' str < <(cat somefile.txt)
echo "$#str"
echo "$str"
This doesn't deal with OP's question, which is really about command substitution, not process substitution.
– codeforester
Apr 24 '18 at 2:01
@codeforester but did this work for you too?
– Aquarius Power
Apr 29 '18 at 0:54
add a comment |
This is another way, good to use with some text editors that are unable to correctly highlight every intricate code you create.
read -r -d '' str < <(cat somefile.txt)
echo "$#str"
echo "$str"
This is another way, good to use with some text editors that are unable to correctly highlight every intricate code you create.
read -r -d '' str < <(cat somefile.txt)
echo "$#str"
echo "$str"
answered May 10 '15 at 21:44
Aquarius PowerAquarius Power
2,03921946
2,03921946
This doesn't deal with OP's question, which is really about command substitution, not process substitution.
– codeforester
Apr 24 '18 at 2:01
@codeforester but did this work for you too?
– Aquarius Power
Apr 29 '18 at 0:54
add a comment |
This doesn't deal with OP's question, which is really about command substitution, not process substitution.
– codeforester
Apr 24 '18 at 2:01
@codeforester but did this work for you too?
– Aquarius Power
Apr 29 '18 at 0:54
This doesn't deal with OP's question, which is really about command substitution, not process substitution.
– codeforester
Apr 24 '18 at 2:01
This doesn't deal with OP's question, which is really about command substitution, not process substitution.
– codeforester
Apr 24 '18 at 2:01
@codeforester but did this work for you too?
– Aquarius Power
Apr 29 '18 at 0:54
@codeforester but did this work for you too?
– Aquarius Power
Apr 29 '18 at 0:54
add a comment |
You can use back-ticks(also known as accent graves) or $()
.
Like as-
OUTPUT=$(x+2);
OUTPUT=`x+2`;
Both have the same effect. But OUTPUT=$(x+2) is more readable and the latest one.
1
bash: x+2: command not found
– F. Hauri
Dec 20 '16 at 7:36
2
Parenthesis was implemented in order to permit nesting.
– F. Hauri
Dec 20 '16 at 7:37
add a comment |
You can use back-ticks(also known as accent graves) or $()
.
Like as-
OUTPUT=$(x+2);
OUTPUT=`x+2`;
Both have the same effect. But OUTPUT=$(x+2) is more readable and the latest one.
1
bash: x+2: command not found
– F. Hauri
Dec 20 '16 at 7:36
2
Parenthesis was implemented in order to permit nesting.
– F. Hauri
Dec 20 '16 at 7:37
add a comment |
You can use back-ticks(also known as accent graves) or $()
.
Like as-
OUTPUT=$(x+2);
OUTPUT=`x+2`;
Both have the same effect. But OUTPUT=$(x+2) is more readable and the latest one.
You can use back-ticks(also known as accent graves) or $()
.
Like as-
OUTPUT=$(x+2);
OUTPUT=`x+2`;
Both have the same effect. But OUTPUT=$(x+2) is more readable and the latest one.
answered Feb 9 '16 at 8:03
Pratik PatilPratik Patil
2,0171424
2,0171424
1
bash: x+2: command not found
– F. Hauri
Dec 20 '16 at 7:36
2
Parenthesis was implemented in order to permit nesting.
– F. Hauri
Dec 20 '16 at 7:37
add a comment |
1
bash: x+2: command not found
– F. Hauri
Dec 20 '16 at 7:36
2
Parenthesis was implemented in order to permit nesting.
– F. Hauri
Dec 20 '16 at 7:37
1
1
bash: x+2: command not found
– F. Hauri
Dec 20 '16 at 7:36
bash: x+2: command not found
– F. Hauri
Dec 20 '16 at 7:36
2
2
Parenthesis was implemented in order to permit nesting.
– F. Hauri
Dec 20 '16 at 7:37
Parenthesis was implemented in order to permit nesting.
– F. Hauri
Dec 20 '16 at 7:37
add a comment |
Some may find this useful.
Integer values in variable substitution, where the trick is using $(())
double brackets:
N=3
M=3
COUNT=$N-1
ARR[0]=3
ARR[1]=2
ARR[2]=4
ARR[3]=1
while (( COUNT < $#ARR[@] ))
do
ARR[$COUNT]=$((ARR[COUNT]*M))
(( COUNT=$COUNT+$N ))
done
1
This does not seem to have any relevance for this question. It would be a reasonable answer if somebody were to ask how to multiply a number in an array by a constant factor, though I don't recall ever seeing anyone asking that (and then afor ((...))
loop would seem like a better match for the loop variable). Also, you should not use uppercase for your private variables.
– tripleee
Dec 28 '15 at 12:22
I disagree with the "relevance" part. The question clearly reads: How to set a variable equal to the output from a command in Bash? And I added this answer as a complement because I got here looking for a solution which helped me with the code I later posted. Regarding the uppercase vars, thanks for that.
– Gus
Dec 28 '15 at 13:38
1
So which command's output are you capturing here?
– tripleee
Dec 28 '15 at 15:30
1
This could be writtenARR=(3 2 4 1);for((N=3,M=3,COUNT=N-1;COUNT < $#ARR[@];ARR[COUNT]*=M,COUNT+=N)) :;
but I agree with @tripleee: I don't understand what do this, there!
– F. Hauri
Dec 20 '16 at 7:25
@F.Hauri... bash is getting more & more like perl the deeper you go into it!
– ropata
Nov 7 '17 at 23:22
add a comment |
Some may find this useful.
Integer values in variable substitution, where the trick is using $(())
double brackets:
N=3
M=3
COUNT=$N-1
ARR[0]=3
ARR[1]=2
ARR[2]=4
ARR[3]=1
while (( COUNT < $#ARR[@] ))
do
ARR[$COUNT]=$((ARR[COUNT]*M))
(( COUNT=$COUNT+$N ))
done
1
This does not seem to have any relevance for this question. It would be a reasonable answer if somebody were to ask how to multiply a number in an array by a constant factor, though I don't recall ever seeing anyone asking that (and then afor ((...))
loop would seem like a better match for the loop variable). Also, you should not use uppercase for your private variables.
– tripleee
Dec 28 '15 at 12:22
I disagree with the "relevance" part. The question clearly reads: How to set a variable equal to the output from a command in Bash? And I added this answer as a complement because I got here looking for a solution which helped me with the code I later posted. Regarding the uppercase vars, thanks for that.
– Gus
Dec 28 '15 at 13:38
1
So which command's output are you capturing here?
– tripleee
Dec 28 '15 at 15:30
1
This could be writtenARR=(3 2 4 1);for((N=3,M=3,COUNT=N-1;COUNT < $#ARR[@];ARR[COUNT]*=M,COUNT+=N)) :;
but I agree with @tripleee: I don't understand what do this, there!
– F. Hauri
Dec 20 '16 at 7:25
@F.Hauri... bash is getting more & more like perl the deeper you go into it!
– ropata
Nov 7 '17 at 23:22
add a comment |
Some may find this useful.
Integer values in variable substitution, where the trick is using $(())
double brackets:
N=3
M=3
COUNT=$N-1
ARR[0]=3
ARR[1]=2
ARR[2]=4
ARR[3]=1
while (( COUNT < $#ARR[@] ))
do
ARR[$COUNT]=$((ARR[COUNT]*M))
(( COUNT=$COUNT+$N ))
done
Some may find this useful.
Integer values in variable substitution, where the trick is using $(())
double brackets:
N=3
M=3
COUNT=$N-1
ARR[0]=3
ARR[1]=2
ARR[2]=4
ARR[3]=1
while (( COUNT < $#ARR[@] ))
do
ARR[$COUNT]=$((ARR[COUNT]*M))
(( COUNT=$COUNT+$N ))
done
answered Nov 22 '15 at 11:59
GusGus
1,55711522
1,55711522
1
This does not seem to have any relevance for this question. It would be a reasonable answer if somebody were to ask how to multiply a number in an array by a constant factor, though I don't recall ever seeing anyone asking that (and then afor ((...))
loop would seem like a better match for the loop variable). Also, you should not use uppercase for your private variables.
– tripleee
Dec 28 '15 at 12:22
I disagree with the "relevance" part. The question clearly reads: How to set a variable equal to the output from a command in Bash? And I added this answer as a complement because I got here looking for a solution which helped me with the code I later posted. Regarding the uppercase vars, thanks for that.
– Gus
Dec 28 '15 at 13:38
1
So which command's output are you capturing here?
– tripleee
Dec 28 '15 at 15:30
1
This could be writtenARR=(3 2 4 1);for((N=3,M=3,COUNT=N-1;COUNT < $#ARR[@];ARR[COUNT]*=M,COUNT+=N)) :;
but I agree with @tripleee: I don't understand what do this, there!
– F. Hauri
Dec 20 '16 at 7:25
@F.Hauri... bash is getting more & more like perl the deeper you go into it!
– ropata
Nov 7 '17 at 23:22
add a comment |
1
This does not seem to have any relevance for this question. It would be a reasonable answer if somebody were to ask how to multiply a number in an array by a constant factor, though I don't recall ever seeing anyone asking that (and then afor ((...))
loop would seem like a better match for the loop variable). Also, you should not use uppercase for your private variables.
– tripleee
Dec 28 '15 at 12:22
I disagree with the "relevance" part. The question clearly reads: How to set a variable equal to the output from a command in Bash? And I added this answer as a complement because I got here looking for a solution which helped me with the code I later posted. Regarding the uppercase vars, thanks for that.
– Gus
Dec 28 '15 at 13:38
1
So which command's output are you capturing here?
– tripleee
Dec 28 '15 at 15:30
1
This could be writtenARR=(3 2 4 1);for((N=3,M=3,COUNT=N-1;COUNT < $#ARR[@];ARR[COUNT]*=M,COUNT+=N)) :;
but I agree with @tripleee: I don't understand what do this, there!
– F. Hauri
Dec 20 '16 at 7:25
@F.Hauri... bash is getting more & more like perl the deeper you go into it!
– ropata
Nov 7 '17 at 23:22
1
1
This does not seem to have any relevance for this question. It would be a reasonable answer if somebody were to ask how to multiply a number in an array by a constant factor, though I don't recall ever seeing anyone asking that (and then a
for ((...))
loop would seem like a better match for the loop variable). Also, you should not use uppercase for your private variables.– tripleee
Dec 28 '15 at 12:22
This does not seem to have any relevance for this question. It would be a reasonable answer if somebody were to ask how to multiply a number in an array by a constant factor, though I don't recall ever seeing anyone asking that (and then a
for ((...))
loop would seem like a better match for the loop variable). Also, you should not use uppercase for your private variables.– tripleee
Dec 28 '15 at 12:22
I disagree with the "relevance" part. The question clearly reads: How to set a variable equal to the output from a command in Bash? And I added this answer as a complement because I got here looking for a solution which helped me with the code I later posted. Regarding the uppercase vars, thanks for that.
– Gus
Dec 28 '15 at 13:38
I disagree with the "relevance" part. The question clearly reads: How to set a variable equal to the output from a command in Bash? And I added this answer as a complement because I got here looking for a solution which helped me with the code I later posted. Regarding the uppercase vars, thanks for that.
– Gus
Dec 28 '15 at 13:38
1
1
So which command's output are you capturing here?
– tripleee
Dec 28 '15 at 15:30
So which command's output are you capturing here?
– tripleee
Dec 28 '15 at 15:30
1
1
This could be written
ARR=(3 2 4 1);for((N=3,M=3,COUNT=N-1;COUNT < $#ARR[@];ARR[COUNT]*=M,COUNT+=N)) :;
but I agree with @tripleee: I don't understand what do this, there!– F. Hauri
Dec 20 '16 at 7:25
This could be written
ARR=(3 2 4 1);for((N=3,M=3,COUNT=N-1;COUNT < $#ARR[@];ARR[COUNT]*=M,COUNT+=N)) :;
but I agree with @tripleee: I don't understand what do this, there!– F. Hauri
Dec 20 '16 at 7:25
@F.Hauri... bash is getting more & more like perl the deeper you go into it!
– ropata
Nov 7 '17 at 23:22
@F.Hauri... bash is getting more & more like perl the deeper you go into it!
– ropata
Nov 7 '17 at 23:22
add a comment |
Here are two more ways:
Please keep in mind that space is very important in bash. So, if you want your command to run, use as is without introducing any more spaces.
following assigns harshil to L and then prints it
L=$"harshil"
echo "$L"following assigns the output of the command
tr
to L2.tr
is being operated on another variable L1.L2=$(echo "$L1" | tr [:upper:] [:lower:])
4
1.$"..."
probably doesn't do what you think it does. 2. This is already given in Andy Lester's answer.
– gniourf_gniourf
Jun 22 '16 at 10:35
@gniourf_gniourf is right: see bash localization won't work with multilines. But under bash, you could useecho $L1,,
to downcase, orecho $L1^^
to upcase.
– F. Hauri
Dec 20 '16 at 7:34
add a comment |
Here are two more ways:
Please keep in mind that space is very important in bash. So, if you want your command to run, use as is without introducing any more spaces.
following assigns harshil to L and then prints it
L=$"harshil"
echo "$L"following assigns the output of the command
tr
to L2.tr
is being operated on another variable L1.L2=$(echo "$L1" | tr [:upper:] [:lower:])
4
1.$"..."
probably doesn't do what you think it does. 2. This is already given in Andy Lester's answer.
– gniourf_gniourf
Jun 22 '16 at 10:35
@gniourf_gniourf is right: see bash localization won't work with multilines. But under bash, you could useecho $L1,,
to downcase, orecho $L1^^
to upcase.
– F. Hauri
Dec 20 '16 at 7:34
add a comment |
Here are two more ways:
Please keep in mind that space is very important in bash. So, if you want your command to run, use as is without introducing any more spaces.
following assigns harshil to L and then prints it
L=$"harshil"
echo "$L"following assigns the output of the command
tr
to L2.tr
is being operated on another variable L1.L2=$(echo "$L1" | tr [:upper:] [:lower:])
Here are two more ways:
Please keep in mind that space is very important in bash. So, if you want your command to run, use as is without introducing any more spaces.
following assigns harshil to L and then prints it
L=$"harshil"
echo "$L"following assigns the output of the command
tr
to L2.tr
is being operated on another variable L1.L2=$(echo "$L1" | tr [:upper:] [:lower:])
edited Aug 7 '18 at 6:58
François Maturel
3,00852536
3,00852536
answered Jun 22 '16 at 10:09
HarshilHarshil
697918
697918
4
1.$"..."
probably doesn't do what you think it does. 2. This is already given in Andy Lester's answer.
– gniourf_gniourf
Jun 22 '16 at 10:35
@gniourf_gniourf is right: see bash localization won't work with multilines. But under bash, you could useecho $L1,,
to downcase, orecho $L1^^
to upcase.
– F. Hauri
Dec 20 '16 at 7:34
add a comment |
4
1.$"..."
probably doesn't do what you think it does. 2. This is already given in Andy Lester's answer.
– gniourf_gniourf
Jun 22 '16 at 10:35
@gniourf_gniourf is right: see bash localization won't work with multilines. But under bash, you could useecho $L1,,
to downcase, orecho $L1^^
to upcase.
– F. Hauri
Dec 20 '16 at 7:34
4
4
1.
$"..."
probably doesn't do what you think it does. 2. This is already given in Andy Lester's answer.– gniourf_gniourf
Jun 22 '16 at 10:35
1.
$"..."
probably doesn't do what you think it does. 2. This is already given in Andy Lester's answer.– gniourf_gniourf
Jun 22 '16 at 10:35
@gniourf_gniourf is right: see bash localization won't work with multilines. But under bash, you could use
echo $L1,,
to downcase, or echo $L1^^
to upcase.– F. Hauri
Dec 20 '16 at 7:34
@gniourf_gniourf is right: see bash localization won't work with multilines. But under bash, you could use
echo $L1,,
to downcase, or echo $L1^^
to upcase.– F. Hauri
Dec 20 '16 at 7:34
add a comment |
protected by Community♦ Apr 19 '13 at 16:00
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
1
A related question stackoverflow.com/questions/25116521/…
– Sandeepan Nath
Aug 25 '16 at 7:09
15
As an aside, all-caps variables are defined by POSIX for variable names with meaning to the operating system or shell itself, whereas names with at least one lowercase character are reserved for application use. Thus, consider using lowercase names for your own shell variables to avoid unintended conflicts (keeping in mind that setting a shell variable will overwrite any like-named environment variable).
– Charles Duffy
Mar 27 '17 at 15:56
As an aside, capturing output into a variable just so you can then
echo
the variable is a useless use ofecho
, and a useless use of variables.– tripleee
Jul 21 '18 at 6:58
As a further aside, storing output in variables is often unnecessary. For small, short strings you will need to reference multiple times in your program, this is completely fine, and exactly the way to go; but for processing any nontrivial amounts of data, you want to reshape your process into a pipeline, or use a temporary file.
– tripleee
Jan 18 at 7:56