Linked Questions

9
votes
3answers
2k views

Why doesn't bash expand this variable when I prefix a command with a “one time variable assignment” [duplicate]

If I run this bash command and prefix the statement, so that the variable fruit should exist, but only for the duration of this command: $ fruit=apple echo $fruit $ The result is an empty line. why?...
0
votes
0answers
24 views

A command scope variable is left assigned after executing the command [duplicate]

Let's check the following script: #!/bin/sh func() { echo "var: $var"; } var="val" func echo "after executing the command, var: $var" Its output is: var: val after executing the command, var: ...
63
votes
6answers
25k views

Why is setting a variable before a command legal in bash?

I've just encountered several answers such as to parsing a delimited text file... that use the construct: while IFS=, read xx yy zz;do echo $xx $yy $zz done < input_file where the IFS ...
40
votes
7answers
40k views

Setting IFS for a single statement

I know that a custom IFS value can be set for the scope of a single command/built-in. Is there a way to set a custom IFS value for a single statement?? Apparently not, since based on the below the ...
9
votes
9answers
4k views

Bash: Extract one of the four sections of an IPv4 address

We can use the syntax ${var##pattern} and ${var%%pattern} to extract the last and first section of an IPv4 address: IP=109.96.77.15 echo IP: $IP echo 'Extract the first section using ${var%%pattern}: ...
22
votes
3answers
3k views

Possible bug in Bash?: foo() { echo “${var[0]}”; }; var=(bar baz) foo

OS: Ubuntu 16.04.3 Shell: Bash 4.3.48 I know that is possible to temporarily change the content of a variable as in var=value command, being probably IFS= read -r var the most notable case of this. ...
1
vote
2answers
4k views

How do I use a temporary environment variable in a bash for loop?

I want to run YII_ENV=prod yii kw/test ten times. I tried $ YII_ENV=prod for x in 1..10 do; yii kw/test done; -bash: for: command not found 1304682651 (Seemed to run once.) I also tried $ for x in {...
7
votes
1answer
820 views

Setting a IFS for a bash script

The typical case is IFS= read. The subject is very well explained in this thread: why is setting a variable before a command legal in bash? In order to have a complete picture, I'd be very grateful if ...
1
vote
1answer
99 views

Is `var=value builtin` the same as `var=value; builtin`?

In my answer here, I used the construct: n=$1 shift Reading Token Recognition spec, I understand that the shell will parse that line into two parts - the variable assignment n=$1 and the builtin ...
0
votes
1answer
55 views

Is using env and export the same thing?

I'm wondering whether this (From https://www.digitalocean.com/community/tutorials/how-to-read-and-set-environmental-and-shell-variables-on-a-linux-vps): env VAR1="blahblah" command_to_run ...