[SOLVED] A bit confused with && and || operators in shell script
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Like bigearsbilly said, parenthesis create a subshell, you should use curly braces instead:
Code:
[ $1 -gt 10 ] && { echo "Greater than ten" && exit 1; } || { echo "Less than or equal to 10" && exit 0; }
echo "Still in the script"
exit 1
Notice that the syntax for curly braces is slightly different, they must be seperated by spaces, and a semicolon or newline is required before the closing brace.
I figure ... using your form, the $PATH is turned into its value by the parent shell and before parsing with IFS set to : whereas in my form the IFS work is done before $PATH is turned into its value so the : characters are not changed into word delimiters. A plausible hypothesis but
Code:
c:~$ IFS=':' /bin/ls /usr/local/bin:/usr/bin:/bin:/usr/games
/bin/ls: cannot access /usr/local/bin:/usr/bin:/bin:/usr/games: No such file or directory
c:~$ IFS=':' /bin/echo "$IFS"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.