Set variable conditionally on command line [duplicate]
This question already has an answer here:
How are parentheses interpreted at the command line?
2 answers
I want to set a variable if my condition is true on my Ubuntu system.
This proves that my if-statement is correct:
$ (if [ 1 == 1 ]; then echo "hi there"; fi);
hi there
This proves that I can set variables:
$ a=1
$ echo $a
1
This shows that setting a variable in the if-statement DOES NOT work:
$ (if [ 1 == 1 ]; then a=2; fi);
$ echo $a
1
Any ideas why? All my google research indicates that it should work like this...
shell command-line
marked as duplicate by RalfFriedl, Thomas, mosvy, Jeff Schaller, muru Nov 21 at 1:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How are parentheses interpreted at the command line?
2 answers
I want to set a variable if my condition is true on my Ubuntu system.
This proves that my if-statement is correct:
$ (if [ 1 == 1 ]; then echo "hi there"; fi);
hi there
This proves that I can set variables:
$ a=1
$ echo $a
1
This shows that setting a variable in the if-statement DOES NOT work:
$ (if [ 1 == 1 ]; then a=2; fi);
$ echo $a
1
Any ideas why? All my google research indicates that it should work like this...
shell command-line
marked as duplicate by RalfFriedl, Thomas, mosvy, Jeff Schaller, muru Nov 21 at 1:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How are parentheses interpreted at the command line?
2 answers
I want to set a variable if my condition is true on my Ubuntu system.
This proves that my if-statement is correct:
$ (if [ 1 == 1 ]; then echo "hi there"; fi);
hi there
This proves that I can set variables:
$ a=1
$ echo $a
1
This shows that setting a variable in the if-statement DOES NOT work:
$ (if [ 1 == 1 ]; then a=2; fi);
$ echo $a
1
Any ideas why? All my google research indicates that it should work like this...
shell command-line
This question already has an answer here:
How are parentheses interpreted at the command line?
2 answers
I want to set a variable if my condition is true on my Ubuntu system.
This proves that my if-statement is correct:
$ (if [ 1 == 1 ]; then echo "hi there"; fi);
hi there
This proves that I can set variables:
$ a=1
$ echo $a
1
This shows that setting a variable in the if-statement DOES NOT work:
$ (if [ 1 == 1 ]; then a=2; fi);
$ echo $a
1
Any ideas why? All my google research indicates that it should work like this...
This question already has an answer here:
How are parentheses interpreted at the command line?
2 answers
shell command-line
shell command-line
edited Nov 20 at 16:19
Barmar
6,9431223
6,9431223
asked Nov 20 at 13:33
Ron
1214
1214
marked as duplicate by RalfFriedl, Thomas, mosvy, Jeff Schaller, muru Nov 21 at 1:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by RalfFriedl, Thomas, mosvy, Jeff Schaller, muru Nov 21 at 1:27
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
The (...)
part of your command is your problem. The parentheses create a separate subshell. The subshell will inherit the environment from its parent shell, but variables set inside it will not retain their new values once the subshell exits. This also goes for any other changes to the environment inside the subshell, including changing directories, setting shell options etc.
Therefore, remove the subshell:
if [ 1 = 1 ]; then a=2; fi
echo "$a"
add a comment |
This proves that setting a variable in a sub-shell has no lasting effect
(if [ 1 == 1 ]; then a=2; fi);
echo $a
produces
1
same as
(a=2)
echo $a
produces
1
Solution remove the parenthesis.
if [ 1 == 1 ]; then a=2; fi;
echo $a
produces
2
or if you need a sub-shell
(
if [ 1 == 1 ]; then a=2; fi;
echo $a
)
produces
2
add a comment |
It done by below method and it worked fine
_example ~]# if [[ 1 == 1 ]]; then echo "praveen"; a=2; echo $a; fi| sed '1i================================n output'
================================
output
praveen
2
4
Can you explain what thesed
command is doing and why you are using it?
– Kusalananda
Nov 20 at 18:49
1
What does this add beyond Kusalananda's very similar, earlier answer and ctrl-alt-delor's earlier answer ?
– Jeff Schaller
Nov 20 at 22:37
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The (...)
part of your command is your problem. The parentheses create a separate subshell. The subshell will inherit the environment from its parent shell, but variables set inside it will not retain their new values once the subshell exits. This also goes for any other changes to the environment inside the subshell, including changing directories, setting shell options etc.
Therefore, remove the subshell:
if [ 1 = 1 ]; then a=2; fi
echo "$a"
add a comment |
The (...)
part of your command is your problem. The parentheses create a separate subshell. The subshell will inherit the environment from its parent shell, but variables set inside it will not retain their new values once the subshell exits. This also goes for any other changes to the environment inside the subshell, including changing directories, setting shell options etc.
Therefore, remove the subshell:
if [ 1 = 1 ]; then a=2; fi
echo "$a"
add a comment |
The (...)
part of your command is your problem. The parentheses create a separate subshell. The subshell will inherit the environment from its parent shell, but variables set inside it will not retain their new values once the subshell exits. This also goes for any other changes to the environment inside the subshell, including changing directories, setting shell options etc.
Therefore, remove the subshell:
if [ 1 = 1 ]; then a=2; fi
echo "$a"
The (...)
part of your command is your problem. The parentheses create a separate subshell. The subshell will inherit the environment from its parent shell, but variables set inside it will not retain their new values once the subshell exits. This also goes for any other changes to the environment inside the subshell, including changing directories, setting shell options etc.
Therefore, remove the subshell:
if [ 1 = 1 ]; then a=2; fi
echo "$a"
edited Nov 20 at 18:50
answered Nov 20 at 13:44
Kusalananda
121k16229372
121k16229372
add a comment |
add a comment |
This proves that setting a variable in a sub-shell has no lasting effect
(if [ 1 == 1 ]; then a=2; fi);
echo $a
produces
1
same as
(a=2)
echo $a
produces
1
Solution remove the parenthesis.
if [ 1 == 1 ]; then a=2; fi;
echo $a
produces
2
or if you need a sub-shell
(
if [ 1 == 1 ]; then a=2; fi;
echo $a
)
produces
2
add a comment |
This proves that setting a variable in a sub-shell has no lasting effect
(if [ 1 == 1 ]; then a=2; fi);
echo $a
produces
1
same as
(a=2)
echo $a
produces
1
Solution remove the parenthesis.
if [ 1 == 1 ]; then a=2; fi;
echo $a
produces
2
or if you need a sub-shell
(
if [ 1 == 1 ]; then a=2; fi;
echo $a
)
produces
2
add a comment |
This proves that setting a variable in a sub-shell has no lasting effect
(if [ 1 == 1 ]; then a=2; fi);
echo $a
produces
1
same as
(a=2)
echo $a
produces
1
Solution remove the parenthesis.
if [ 1 == 1 ]; then a=2; fi;
echo $a
produces
2
or if you need a sub-shell
(
if [ 1 == 1 ]; then a=2; fi;
echo $a
)
produces
2
This proves that setting a variable in a sub-shell has no lasting effect
(if [ 1 == 1 ]; then a=2; fi);
echo $a
produces
1
same as
(a=2)
echo $a
produces
1
Solution remove the parenthesis.
if [ 1 == 1 ]; then a=2; fi;
echo $a
produces
2
or if you need a sub-shell
(
if [ 1 == 1 ]; then a=2; fi;
echo $a
)
produces
2
edited Nov 20 at 14:12
Zanna
2,5461023
2,5461023
answered Nov 20 at 13:42
ctrl-alt-delor
10.8k41957
10.8k41957
add a comment |
add a comment |
It done by below method and it worked fine
_example ~]# if [[ 1 == 1 ]]; then echo "praveen"; a=2; echo $a; fi| sed '1i================================n output'
================================
output
praveen
2
4
Can you explain what thesed
command is doing and why you are using it?
– Kusalananda
Nov 20 at 18:49
1
What does this add beyond Kusalananda's very similar, earlier answer and ctrl-alt-delor's earlier answer ?
– Jeff Schaller
Nov 20 at 22:37
add a comment |
It done by below method and it worked fine
_example ~]# if [[ 1 == 1 ]]; then echo "praveen"; a=2; echo $a; fi| sed '1i================================n output'
================================
output
praveen
2
4
Can you explain what thesed
command is doing and why you are using it?
– Kusalananda
Nov 20 at 18:49
1
What does this add beyond Kusalananda's very similar, earlier answer and ctrl-alt-delor's earlier answer ?
– Jeff Schaller
Nov 20 at 22:37
add a comment |
It done by below method and it worked fine
_example ~]# if [[ 1 == 1 ]]; then echo "praveen"; a=2; echo $a; fi| sed '1i================================n output'
================================
output
praveen
2
It done by below method and it worked fine
_example ~]# if [[ 1 == 1 ]]; then echo "praveen"; a=2; echo $a; fi| sed '1i================================n output'
================================
output
praveen
2
edited Nov 20 at 22:54
ilkkachu
55.7k783152
55.7k783152
answered Nov 20 at 18:19
Praveen Kumar BS
1,206138
1,206138
4
Can you explain what thesed
command is doing and why you are using it?
– Kusalananda
Nov 20 at 18:49
1
What does this add beyond Kusalananda's very similar, earlier answer and ctrl-alt-delor's earlier answer ?
– Jeff Schaller
Nov 20 at 22:37
add a comment |
4
Can you explain what thesed
command is doing and why you are using it?
– Kusalananda
Nov 20 at 18:49
1
What does this add beyond Kusalananda's very similar, earlier answer and ctrl-alt-delor's earlier answer ?
– Jeff Schaller
Nov 20 at 22:37
4
4
Can you explain what the
sed
command is doing and why you are using it?– Kusalananda
Nov 20 at 18:49
Can you explain what the
sed
command is doing and why you are using it?– Kusalananda
Nov 20 at 18:49
1
1
What does this add beyond Kusalananda's very similar, earlier answer and ctrl-alt-delor's earlier answer ?
– Jeff Schaller
Nov 20 at 22:37
What does this add beyond Kusalananda's very similar, earlier answer and ctrl-alt-delor's earlier answer ?
– Jeff Schaller
Nov 20 at 22:37
add a comment |