Share your knowledge at the LQ Wiki.
Go Back > Blogs > linux-related notes
User Name


Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Rate this Entry

Pay attention to ";" versus "&&" in bash aliases

Posted 09-27-2017 at 03:54 PM by the dsc
Updated 09-27-2017 at 03:57 PM by the dsc

Or maybe avoid it altogether, it's probably some sort of well known linux/unix rule that someone wrote a long time ago.

It's possibly something that seldom ever is a problem to anyone, but it was for me.

I had this as an alias for "mv"

echo "use mv -i to avoid overwriting files with the same name; this is an alias to mv -i" ; mv -i
Then at some point I was doing an one liner that would scan symlinks' "realpaths" to decide whether to move them or not, and the result was that it was moving regardless of a path being grepped or not, despite of a "dry run" with "echo mv" suggesting otherwise.

That's "obviously" because the ";" instead of "&&", as well as not using if/then/fi to do the checking, but just "grep pattern &&". With that alias, the de facto one liner was then:

grep pattern && echo "warning message on alias" ; mv $file ./folder
In other words, not grepping "pattern" just skipped the echo part of the alias, but still had the "mv" afterwards. With "&&" on the alias it worked, more or less as if it were done with a proper if/then/fi statement, which is possibly preferably the ideal thing to do most of the time.

I'm adding also "&& sleep 1" on the alias, so to force myself actually using "mv -i", or my silent alias, "mvi".
Posted in Uncategorized
Views 143 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 12:30 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration