Questions tagged [assignment]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
34
votes
2answers
7k views

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 ...
12
votes
3answers
5k views

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?
10
votes
3answers
23k views

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 SERVER_LOGIN=foobar123@prod....
10
votes
2answers
499 views

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) || ...
8
votes
2answers
3k views

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 ...
6
votes
1answer
229 views

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 ...
5
votes
4answers
786 views

Assign the same string to multiple variables

I want to assign the string contained in $value to multiple variables. Actually the example I gave before (var1=var2=...=$value) was not reflecting exactly what I wanted. So far, I found this but it ...
5
votes
1answer
2k views

Any problem assigning one variable to another in shell without using quotes? [duplicate]

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. ...
5
votes
2answers
14k views

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 ...
4
votes
4answers
763 views

Assign variable using multiple lines

I have a function f(){ echo 777 } and a variable to which I assign the "return value" of the function. x=$(f) Very concise! However, in my real code, the variable and function names are quite ...
4
votes
4answers
1k views

Why is foo=bar different from foo= bar? [duplicate]

What is the interpretation of the whitespace in this command foo= bar ? Why are foo=bar and foo= bar interpreted differently Example (Ubuntu bash) developer@1604:~$ foo=bar developer@1604:~$ foo= ...
3
votes
3answers
755 views

What is the difference of defining a variable with or without quotation marks? [duplicate]

If I define a variable with the quotation mark: TEMP="~/Dropbox" then ls $TEMP would not work, instead echo $TEMP | ls works. And to get the same result, I can also define the variable without ...
3
votes
1answer
77 views

$-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 ...
3
votes
1answer
396 views

Bash: space as a key in an associative array

This code in Bash s="There are seven pencil" declare -A A while IFS= read -rn1 a; do [ -z "$a" ] || [ -n "${A[$a]}" ] && continue printf %s "$a" ((A[$a]++)) # A[$a]=x done <<<"$...
3
votes
2answers
1k views

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= ...
2
votes
2answers
218 views

Why is it not required to double quote `$bar` in assignment `foo=$bar`? [duplicate]

From https://unix.stackexchange.com/a/32227/674 The main places where it's safe not to use the double quotes are: in an assignment: foo=$bar (but note that you do need the double quotes in ...
1
vote
1answer
57 views

Output in variable shouldn't be interpreted as command [duplicate]

So this question was asked some times already, but the answers didn't seem to work for me. So I have this simple script #!/bin/bash charon_id = $(pidof charon) kill -1 $charon_id And console says ...
1
vote
1answer
170 views

Different command outputs

I entered same command with a variable host. During 2nd time I entered blank space between host,= and $. Why it was trying to connect to a server ? Also, when I write like $(host name), I am getting ...
1
vote
1answer
106 views

create an array

I want to learn about arrays and how to assign values to them, so I found this tutorial While running the following script: #!/bin/bash $names=([0]="Bob" [1]="Peter" [20]="$USER" [21]="Big Bad John")...
1
vote
2answers
2k views

One line env variable set behavior [duplicate]

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?
1
vote
1answer
39 views

What options are available to copy date output to a variable? [closed]

Context: router outfitted with Merlin AsusWRT. The goal is to append the epoch time to a text file from the UNIX command-line within router. Example: {date:1532348491} The date command return ...
1
vote
0answers
79 views

How can I run speech to text and save the result in a variable?

I would like to speak into my computer's microphone, have what I say converted to text and then have that available as a shell variable. Is this possible? I thought I might do it using Google's ...
1
vote
0answers
86 views

How do I assign a value to the Path key in a .desktop file including the $HOME environment variable correctly?

I'm making a .desktop file for Minecraft. Nothing appears to happen upon executing the it. The Exec and Path keys in the file: Exec=java -jar Minecraft.jar Path=$HOME/.minecraft/ I've also tried ...
0
votes
3answers
902 views

Bash - Set a variable [duplicate]

I need to set a variable in my bash script #!/usr/bin/env bash GITNAME= git config --global user.name echo " $GITNAME " But it doesn't seems to work that way. How does it work?
0
votes
2answers
54 views

Bash String Manipulation and Assignment

How can I manipulate a string and then assign it to a variable? This string manipulation works as I want: echo ${dir:2:5} | sed 's/[.]$//'; but: var x = ${dir:2:5} | sed 's/[.]$//'; and x = ${...
-3
votes
1answer
146 views

Lexical analysis for assignment in a POSIX shell

For the lexical analysis in a POSIX shell, from http://pubs.opengroup.org/onlinepubs/007904875/utilities/xcu_chap02.html 7. [Assignment preceding command name] a. [When the first word] ...
-5
votes
1answer
41 views

Why is unquoted $PATH containing whitespaces safe in an assignment? [duplicate]

What does the following from https://unix.stackexchange.com/a/154290/674 mean? In general, in shells other than zsh, $PATH outside double quotes breaks when the value contains spaces or other ...