Why is only the first line evaluated with IFS=' ' [duplicate] - bash

This question already has answers here:
Unix read command with option -d and IFS variable combination
(2 answers)
What does the bash read -d '' do?
(2 answers)
Bash: assignment of variable on same line not altering echo behavior [duplicate]
(2 answers)
Bash variable assignment before command [duplicate]
(2 answers)
Closed 2 years ago.
I'm trying to split a multi-line string on spaces only, preserving line breaks:
IFS=' ' read a b c <<< "$(printf '%s\n' "foo" "bar" "baz")"; echo "a=[$a]"; echo "b=[$b]"; echo "c=[$c]"
Expected:
a=[foo
bar
baz
]
b=[]
c=[]
Actual:
a=[foo]
b=[]
c=[]
What am I missing? When I replace \n with \t it works as expected.
I'm using bash v5.0.18

Related

How do I retrieve * literal using read command? [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Printing asterisk ("*") in bash shell
(2 answers)
Closed 2 years ago.
I have a script that receives input from the user. One of the possible values I want to retrieve is the literal *. It seems the shell is intercepting it and executing so that I'm getting a list of all the file names.
while read -p "Enter operand: " operandTwo
echo "Argument: $operandTwo"
echo "Argument: $(echo $operandTwo)"
[ $operandTwo != X ]
echo "Result: $?"
do...
This is the output:
Argument: *
Argument: File1 File2 File3
./script: line 20: [: too many arguments
Result: 2
Is the comparison being made here between File1 File2 File3 and X? How do I make a comparison between * and X instead?

Print blank space in bash script [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
How to store output from printf with formatting in a variable? [duplicate]
(2 answers)
Closed 4 years ago.
Why such script:
x=xxx
y=yyy
printf -v var "%s %s" "$x" "$y"
printf $var
prints only:
xxx
While I expected:
xxx yyy
How to force printf not ignore symbols after blank space?
How about
#!/bin/bash
x=xxx
y=yyy
var=`printf "%s %s" $x $y`
echo $var
?

Unexpected end of file bash [duplicate]

This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Closed 5 years ago.
lines=`grep -c "" List`
a=1
while [$a -lt $lines]
do
b=`sed "${a} q;d" List`
a=$a+1
echo $b
done
So, I have this, what is supposed to take a line from a list and echo it.
Now, the while loop is not working(?) and returns me this error:HARR.sh: line 9: syntax error: unexpected end of file
What is wrong with it?
Is this what you want?
#!/bin/bash
#
let a=1
while read line; do
echo "$a $line"
let a+=1
done < List

Shell scripting while loop error [duplicate]

This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
#!/bin/bash
a=0
while ["$a" -lt 10]
do
a=`expr "$a" + 1`
echo $a
done
This is the error:
a.sh: 3: a.sh: [0: not found
you need to keep spaces around your brackets:
while [ "$a" -lt 10 ]
since bash interpretes strings; it uses spaces to separate words. When you write ["$a"; then bash reads: [0 (after $a is replaced with 0)

How to preserve trailing whitespace in bash function arguments? [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 3 years ago.
Consider the following bash script:
#!/bin/bash
function foo {
echo -n $1
echo $2
}
foo 'Testing... ' 'OK' # => Testing...OK
# Whitespace --^ ^
# Missing whitespace -----------------^
What happened to the trailing whitespace in the first argument? How can preserve it?
What happened to the trailing whitespace in the first argument?
The whitespace was included on the echo command line, but was discarded by the shell, the same as if you had typed:
echo -n Testing...
^
|----- there is a space here
How can preserve it?
Quote your variables:
function foo {
echo -n "$1"
echo "$2"
}

Resources