Issue
The following lines will execute the first command but I'm not sure how to cut the 3rd letter from the output
#!/bin/bash
cat << EFO
Hello
World
How are you
EFO
&&
while read hello.sh
do
echo ${hello.sh} | cut -c3
done
Expecting the below output:
l
r
w
Solution
That's a useless cat
and a useless while read
. cut
already knows how to read its own standard input.
cut -c3 <<EOF
Hello
World
How are you
EOF
The here document's delimiter can be any string really, but the conventional "EOF" stands for "end of file".
Also, you can't have dots in your variable names, and you should wrap quotes around your variables unless you require the shell to perform whitespace splitting and wildcard expansion on the value. Finally, you want to use read -r
unless you specifically want the legacy pre-POSIX behavior of read
when it receives a backslash.
https://shellcheck.net/ can report and even fix many beginner problems like these; probably try it before asking for human assistance.
Syntactically, a && b
means run a
and check whether it succeeded; if it did, also run b
. You seem to be looking for a | b
which says "run a
and b
, with the standard output of a
connected as b
's standard input".
Answered By - tripleee Answer Checked By - Marilyn (WPSolving Volunteer)