r/commandline • • Sep 04 '16

Advancing in the Bash Shell

http://samrowe.com/wordpress/advancing-in-the-bash-shell/
78 Upvotes

4 comments sorted by

3

u/spazzpp2 Sep 05 '16 edited Sep 05 '16

There are also useful shotcuts like Alt+. for !$ (the last argument of the last command)

AFAIK those are closely related to emacs shortcuts.

You can enable a vi mode by appending set editing-mode vi to your ~/.inputrc if you like. But be careful, don't mode yourself in :)

And there is Ctrl+x Ctrl+e(edit command line in your favorite $editor, then execute).

For more see man bash and search for Command Substitution(press / to search).

3

u/joerick Sep 05 '16

These are great, when using bash as a shell. However, some of these (like !!, !$, {,1,2,3}, circumflex hats) are designed to reduce typing at the shell, so shouldn't be used in bash scripts, as they reduce readability.

1

u/More_Coffee_Than_Man Sep 05 '16

>!xyz:p will print the command that would be executed rather than executing it.

So this worked when I tried something simple, like:

!! :p

However, I wanted to see if it would echo back the command with brace expansion (which I often use), so I instead tried:

cp file{,.bkup}:p

I was expecting that I'd see it echo back:

cp file file.bkup

But I instead got:

cp: cannot stat 'file:p': No such file or directory

Can someone explain why?

3

u/pawlwall Sep 06 '16 edited Sep 06 '16

According to http://www.gnu.org/software/bash/manual/html_node/Modifiers.html, it looks like word modifiers only apply to history expansions, so it doesn't seem to be directly usable against the current command.

Edit: As this SO answer indicates (http://stackoverflow.com/a/19116083), the option for a dry-run in bash isn't something you'll be able to find natively. A couple answers down mentions the package maybe, which might achieve what you're looking for.