Print blank space in bash script [duplicate] - bash

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
?

Related

search for a variable in a string in bash [duplicate]

This question already has answers here:
How to check if a string contains a substring in Bash
(29 answers)
Closed 6 months ago.
I have a string given below:
string1 = "Hello there, my name is Jack.
Hello there, my name is Jack.
Hello there, my name is Jack."
I'm taking the following input from the string:
read string2
I want to check whether the string2(which is a variable) is present in string1.
I tried running the below command:
output=$(echo $string1 | grep -o "$string2")
echo $output
eg: Let string2="name"
The output is empty when I'm running this command.
Can someone tell me where am I going wrong?
#!/bin/bash
string1="Hello there, my name is Jack"
string2="name"
if [[ $string1 == *"$string2"* ]]; then
echo "$string2 found"
else
echo "$string2 not found"
fi
Alternate method with POSIX-shell grammar:
string1='Hello there, my name is Jack'
string2='name'
case "$string1" in
*"$string2"*) printf '%s found\n' "$string2";;
*) printf '%s not found\n' "$string2";;
esac

BASH:Why is printf to a var removing spaces? [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 2 years ago.
Why is the output of these two different? I want the spaces to be preserved.
#!/bin/bash
x="foo"
printf "%-12s *" $x
echo " "
y=$(printf "%-12s" $x)
echo $y "*"
Running it gives
foo *
foo *
I want the second line to look like the first.
You'll need to quote the $y:
echo "${y} *"
#!/bin/bash
x="foo"
printf "%-12s *" $x
echo " "
y=$(printf "%-12s" $x)
echo "$y" "*"
➜ ./test.sh
foo *
foo *
➜
More information about when to use double quotes
Ypu can use printf too
printf '%s *\n' "$y"

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

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

Comparing two variables in IF condition inside While loop in bash script [duplicate]

This question already has answers here:
Bash comparison operator always true
(1 answer)
How to assign the output of a Bash command to a variable? [duplicate]
(5 answers)
Closed 2 years ago.
i am trying to execute a IF condition inside a while loop, But the IF condition isn't working as variables aren't expanding! kindly guide me through the proper way to compare two variables in IF condition
Note - if you can see the error log - DATE was expanding thou! problem with mdate
DATE=`date +"%Y-%m-%d"`
cat path/temp_b | while read file
do
echo 'phase2'
mtime=$(stat -c '%y' $Src_Dir/$file)
echo $mtime
mdate= echo $mtime | cut -d ' ' -f1
echo $mdate
echo $DATE
if ["$mdate"=="$DATE"]; then
"$file" > path/tempc
else
echo 'hi'
fi
done
**Error log -
phase2
2020-05-07 05:22:28.000000000 -0400
2020-05-07
2020-07-21
./test1.ksh: line 37: [==2020-07-21]: command not found
hi**
Change your if statement like:
if [ "$mdate" == "$DATE" ]; then
Explanation: if in bash needs to have square brackets, operator, and operand to be space-separated.

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

Resources