Questions tagged [assignment]
40 questions
50
votes
2 answers
Are quotes needed for local variable assignment?
Can I safely omit quotes on the right side of a local assignment?
function foo {
local myvar=${bar}
stuff()
}
I'm mainly interested in bash, but any info on corner cases in other shells are welcome.
rahmu
- 19,673
- 28
- 87
- 128
38
votes
3 answers
Spaces in variable assignments in shell scripts
What is the difference between below variables assignments?
var=23
var =23
var= 23
var = 23
Is there any difference in space around the assignment operator?
ajay
- 421
- 1
- 5
- 7
26
votes
3 answers
Conditional assignment based on environment variable
In a bash script, I'm assigning a local variable so that the value depends on an external, global environment variable ($MYAPP_ENV).
if [ "$MYAPP_ENV" == "PROD" ]
then
[email protected]
else
…
Jonik
- 1,460
- 3
- 13
- 17
18
votes
3 answers
Zsh: export: not valid in this context
When running this script, I run into an error on this line (relevant snippet below):
...
_NEW_PATH=$("$_THIS_DIR/conda" ..activate "$@")
if (( $? == 0 )); then
export PATH=$_NEW_PATH
# If the string contains / it's a path
if [[ "$@" ==…
Amelio Vazquez-Reina
- 40,169
- 77
- 197
- 294
15
votes
2 answers
One line env variable set behavior
If I do this:
SUMAN_DEBUG=foo echo $SUMAN_DEBUG
I get no output
but if I do:
SUMAN_DEBUG=foo && echo $SUMAN_DEBUG
then I get
"foo"
why is that?
Alexander Mills
- 9,330
- 19
- 95
- 180
12
votes
2 answers
Assignments are like commands with an exit status except when there's command substitution?
See the following examples and their outputs in POSIX shells:
false;echo $? or false || echo 1: 1
false;foo="bar";echo $? or foo="bar" && echo 0: 0
foo=$(false);echo $? or foo=$(false) || echo 1: 1
foo=$(true);echo $? or foo=$(true) && echo 0:…
phk
- 5,893
- 7
- 41
- 70
11
votes
2 answers
Variable assignment outside of case statement
In many languages it is possible to assign the result of a case/switch statement to a variable, rather than repeating the variable assignment many times within the case statement. Is it possible to do something like this in the Bash…
iconoclast
- 9,057
- 12
- 56
- 95
9
votes
2 answers
In POSIX scripting, is x=$y always equivalent to x="$y"?
Are
x=$y
and
x="$y"
always equivalent? Didn't know how to search for this. So far, I've always been using x="$y" to be "on the safe side". But I used x=$1 at one point and noticed that, apparently, I don't even need the extra double quotation…
finefoot
- 2,940
- 2
- 21
- 41
9
votes
5 answers
Assign the same string to multiple variables
I want to assign the string contained in $value to multiple BASH variables.
Actually the example I gave before (var1=var2=...=$value) was not reflecting exactly what I wanted.
So far, I found this but it only works if $value is an integer:
$ let…
SebMa
- 1,941
- 4
- 22
- 37
7
votes
2 answers
changing IFS temporarily before a for loop
I know that the SHELL allows variable assignment to take place immediately before a command, such that IFS=":" read a b c d <<< "$here_string" works...
What I was wondering is do such assignments not work when done with compound statements such as…
computronium
- 778
- 6
- 15
7
votes
1 answer
Unexpected outcome of a="$@"
I'm struggling with this situation:
$ set -- 1 2 3
$ a="$@"
$ echo "$a"
1 2 3
What I find unexpected is the assignment itself.
man bash says this about the "$@" expansion:
When the expansion occurs within double quotes, each parameter expands
to…
user147505
6
votes
2 answers
why is zsh not expanding this glob defined inside a function '(:a)'
The second line of the script only works if I trigger glob expansion by executing echo. I can't understand why. Here's the command and it's execution to give some context.
Function definition:
~/ cat ~/.zsh/includes/ascii2gif
ascii2gif () {
…
bryan hunt
- 63
- 3
6
votes
2 answers
How do I write a command for the Exec key in a .desktop file containing a reserved character correctly?
I'm trying to make a .desktop file for Minecraft. Nothing appears to happen upon executing the file. I've tried assigning the Exec key as follows:
Exec= java -jar "~/.minecraft/Minecraft.jar"
Exec= java -jar "$HOME/.minecraft/Minecraft.jar"
But I'm…
reggie-man
- 83
- 1
- 5
5
votes
1 answer
Any problem assigning one variable to another in shell without using quotes?
This question is about assigning the entire contents of one variable to another variable.
Variable is not being used (sent to echo, etc.)
No parameter expansion is being done during the assignment.
My question also pertains only to POSIX shell (no…
Caleb Paul
- 159
- 1
- 5
4
votes
1 answer
$-expansions not performed as expected when following a redirection
It seems that bash and zsh will perform the variable and arithmetic expansions in a child process when
a) they're following a redirection operator like <, >, >> or <<<.
b) the command they're part of is not a built-in or function.
bash -c 'i=0;…
user313992