DEV Community

Discussion on: Shellscripting: Conditional Execution

Collapse
 
polyluxus profile image
Martin Schwarzer

You should use exit statuses directly, not via $?. Like

if command ; then stuff ; fi

Shorter, and save from accidentally picking up the wrong exit code.

Also var = $(echo "$((2+2))") spawns two sub shells, where you only need one: var=$((2+2))

Collapse
 
_jack_t_ripper_ profile image
Jack t Ripper

I make it a practice to always use echo and double quotes so that everything inside is resolved like the variables and maybe it is overkill but it is due to habit and also errors that I have seen that come up not using that way in various enviornments

And again putting the
if command;

May lead to again problems with variables and escaping of escapable characters.

Escaping and variables plus nested commands is why I do both practices,.

Thread Thread
 
polyluxus profile image
Martin Schwarzer

It's not just overkill, it's useless, and it might have some nasty side effects, too. Here's some more explanation: github.com/koalaman/shellcheck/wik...

If you're having trouble with your variables and escaping characters, you could always fun it in a sub shell and use that error code in the if construct. You're making it overly complicated, and prone to errors.

Thread Thread
 
_jack_t_ripper_ profile image
Jack t Ripper • Edited

Idk why everyone has to argue shit on the internet, your own citations says that using echo has its places, and I use what works for me... Not making it complicated just making something that I know works... Just because there is something that also works doesn't make how mine worked wrong...