News for hackers. Stuff that matters.
Re:How to navigate directories faster with Bash (2015) by Normille on Thursday April 22, 2021 @02:57AM (#26899626)
I upvoted for this alone:

  Another neat short hand is !$ which is an alias for the last argument of the previous command. This can be handy if one creates a new directory and wants to change into it without typing the the directory again. So commands would be

  mkdir -p make/new/directory
  cd !$
You've no idea how many years I've been thinking "This is Linux fer gawd's sake. Surely there must be some easy way to make a new directory and 'cd' to it in a single command!" But was never able to find or guess the right incantation.
Re:How to navigate directories faster with Bash (2015) by RhysU on Thursday April 22, 2021 @07:44AM (#26901631)
I have an "mkcd" function in my startup files:

mkcd() { mkdir -vp "$1" && cd "$1"; }

Re:How to navigate directories faster with Bash (2015) by alsetmusic on Thursday April 22, 2021 @08:45AM (#26902096)
Wait, now I'm confused. I have:

mkcd() { mkdir -p "$@" && cd "$_"; }

Can someone please detail the differences between $! and $_? (Apologies if I'm missing something obvious)

Re:How to navigate directories faster with Bash (2015) by tyingq on Thursday April 22, 2021 @08:59AM (#26902228)

  $ echo "test" > /dev/null && echo !$
  /dev/null
  $ echo "test" > /dev/null && echo $_
  test
Re:How to navigate directories faster with Bash (2015) by karatinversion on Thursday April 22, 2021 @09:05AM (#26902288)
You're thinking of $_ and !$, and I think they are equivalent - $_ is explicitly the last argument of the previous command, whereas !$ is a history expansion command. ! starts a new history expansion command, and $ is a word designator for the last word of the selected history entry. To quote man bash, " If a word designator is supplied without an event specification, the previous command is used as the event."

I would not that !$ is a bash-ism, whereas $_ works at least on zsh and ksh as well as bash.

Re:How to navigate directories faster with Bash (2015) by forgotmypw17 on Thursday April 22, 2021 @03:58PM (#26907436)

     alias tcd='a=`date +%s`;mkdir "$a" && cd "$a"'
Re:How to navigate directories faster with Bash (2015) by gpspake on Thursday April 22, 2021 @08:32AM (#26901988)
Yep, first thing I add to my dotfiles when I don't have it haha
Re:How to navigate directories faster with Bash (2015) by cutthegrass2 on Thursday April 22, 2021 @07:54AM (#26901705)
+1, I do the same.
Re:How to navigate directories faster with Bash (2015) by bunsenhoneydew on Thursday April 22, 2021 @08:05AM (#26901771)
Me too
Re:How to navigate directories faster with Bash (2015) by lolive on Thursday April 22, 2021 @03:18AM (#26899760)
Typing Esc then . does the trick too.
Re:How to navigate directories faster with Bash (2015) by alex_smart on Thursday April 22, 2021 @04:00AM (#26900075)
Alt + . does the same thing as well, with the added advantage that you can keep pressing alt and then hit . several times to go through all the "last arguments" in your history one by one.

Further, holding Alt followed by numeric argument followed by dot, gives you an argument at a specific position. For example, Alt + 1, Alt + . copies the first argument.

Re:How to navigate directories faster with Bash (2015) by seesawtron on Thursday April 22, 2021 @04:39AM (#26900322)
Cool one. "Ctrl + R" also does the same thing to search through past commands and then "Tab" to select the command you are looking for.
Re:How to navigate directories faster with Bash (2015) by alex_smart on Thursday April 22, 2021 @05:10AM (#26900516)
May favorite bash "trick" is:

!-1:gs/hello/world

which runs the last command with all instances of hello replaced by world. !-1 can be any so called event designator, for instance !n to refer to any specific line number from history or !string for last command containing a particular string.

Seriously people, read the man page for bash. Your shell has so many cool features.

Re:How to navigate directories faster with Bash (2015) by tikkabhuna on Thursday April 22, 2021 @05:53AM (#26900774)
And if you only have a single instance to replace, you can use ^old^new^

$ echo "hello hello hello world" hello hello hello world $ ^hello^bonjour^ echo "bonjour hello hello world" bonjour hello hello world

Re:How to navigate directories faster with Bash (2015) by alex_smart on Thursday April 22, 2021 @06:16AM (#26900981)
I know that this expansion exists, but never use it in practice.

Why? The last command is more general, you can just drop the g from the command I wrote to only replace the first occurrence. Besides, :[g]s/old/new should already be familiar to anyone who has used vim, so that is almost like having to learn zero new things.

Re:How to navigate directories faster with Bash (2015) by dcassett on Thursday April 22, 2021 @08:19AM (#26901876)
This is great for removing an option from a prior command such as unzip -t ...:

^-t

Re:How to navigate directories faster with Bash (2015) by pupdogg on Thursday April 22, 2021 @06:38AM (#26901164)
[ESC + .] and [ALT + .] are most definitely news ones for me. Thank you. Always learn something new here!
Re:How to navigate directories faster with Bash (2015) by loulouxiv on Thursday April 22, 2021 @09:00AM (#26902242)
Re:How to navigate directories faster with Bash (2015) by alex_smart on Thursday April 22, 2021 @12:29PM (#26904665)
Thanks, I discovered C-o thanks to you (even though I have gone through the bash manual several times).

No, more having to press C-r multiple times just to get sequential group of commands from history.

Re:How to navigate directories faster with Bash (2015) by loulouxiv on Thursday April 22, 2021 @06:41PM (#26909004)
Yes I was also impressed when I found out about this one. However I use it far less frequently than ALT-. . ALT-# is also useful in the frequent cases where I am constructing a complicated commmand but I have to run another command before the current one.

Also keep in mind that most interactive command-line tools are built upon readline and can understand all those bindings (think python REPL, mysql/psql...)

Re:How to navigate directories faster with Bash (2015) by alex_smart on Thursday April 22, 2021 @08:27PM (#26909723)
>However I use it far less frequently than ALT-.

Haha of course. I use it so frequently I can't imagine how anyone uses their shell without it.

>can understand all those bindings

I knew that but just being reminded helped me realize that C-o is probably going to be useful in the REPL as well. Thanks.

Re:How to navigate directories faster with Bash (2015) by loulouxiv on Thursday April 22, 2021 @09:02AM (#26902255)
or man bash /Readline Command Names<Enter>
Re:How to navigate directories faster with Bash (2015) by rswail on Thursday April 22, 2021 @05:22AM (#26900588)
TIL! Oh this one is nice... now just to get my fingers to remember it.
Re:How to navigate directories faster with Bash (2015) by madacol on Thursday April 22, 2021 @04:39AM (#26900321)
Re:How to navigate directories faster with Bash (2015) by mraza007 on Thursday April 22, 2021 @04:56AM (#26900430)
I have been looking for this Thanks for sharing :)
Re:How to navigate directories faster with Bash (2015) by emmelaich on Thursday April 22, 2021 @08:33AM (#26901995)
Only in emacs editing mode.

Real programmers use set -o vi

Re:How to navigate directories faster with Bash (2015) by stjohnswarts on Thursday April 22, 2021 @05:06PM (#26908199)
I use that on test machines a lot and had put it in bashrc and heard some cussing from other devs so now I just have to type it every time I spend more than acouple seconds in a terminal :) .
Re:How to navigate directories faster with Bash (2015) by gadiyar on Thursday April 22, 2021 @03:39AM (#26899902)
I didn't know this. Really neat!
Re:How to navigate directories faster with Bash (2015) by itsmarsu on Thursday April 22, 2021 @03:51AM (#26899999)
That's just magical
Re:How to navigate directories faster with Bash (2015) by thomasahle on Thursday April 22, 2021 @03:55AM (#26900035)
How does that work?
Re:How to navigate directories faster with Bash (2015) by saagarjha on Thursday April 22, 2021 @04:09AM (#26900127)
M-. is readline's yank-last-arg binding.
Re:How to navigate directories faster with Bash (2015) by a3n on Thursday April 22, 2021 @06:36AM (#26901155)
Nice.

Bash is more than just bash. Composition is a killer technique.

Re:How to navigate directories faster with Bash (2015) by nmfisher on Thursday April 22, 2021 @04:11AM (#26900146)
You just blew my mind.
Re:How to navigate directories faster with Bash (2015) by teptoria on Thursday April 22, 2021 @03:49AM (#26899980)
Wow amazing! Thanks :)
Re:How to navigate directories faster with Bash (2015) by cerved on Thursday April 22, 2021 @03:25AM (#26899799)
what, what?
Re:How to navigate directories faster with Bash (2015) by W0lf on Thursday April 22, 2021 @05:16AM (#26900549)
For this exact use case I defined the following many years ago in my bash profile:

    # create a new directory and enter it
    function mkd() {
        mkdir -p "$@" && cd "$_";
    }
And use it like: mkd foo
Re:How to navigate directories faster with Bash (2015) by mfontani on Thursday April 22, 2021 @08:46AM (#26902114)
Beware that using "$@" there might not be what you want, as it passes space-separated arguments to mkdir.

What does: "mkd foo bar" do? It creates both directories, and cd's into "bar".

Might be worth checking that only one argument has been passed, or use "$*" which instead would treat all arguments as one parameter, and "mkd foo bar" would create a "foo bar" directory instead (and cd to it)

YMMV.

Re:How to navigate directories faster with Bash (2015) by W0lf on Thursday April 22, 2021 @03:17PM (#26906931)
Thanks a lot for pointing this out. I will happily fix this now!
Re:How to navigate directories faster with Bash (2015) by dvfjsdhgfv on Thursday April 22, 2021 @10:05AM (#26902891)
All Bash manuals and tutorials should start from explaining this issue. And I really mean it. I can't remember how many times I was bitten by it until I finally read it up.
Re:How to navigate directories faster with Bash (2015) by mfontani on Thursday April 22, 2021 @12:45PM (#26904870)
I feel one should also install shellcheck, and follow it religiously while looking up every warning they get - to understand what it's about, why it's a problem, and why the solution / "correct" incantation is so.

It's also a good way to "learn by doing", which works for many.

Re:How to navigate directories faster with Bash (2015) by samus on Thursday April 22, 2021 @11:42AM (#26904028)
I still have to read it up from time to time even though I know it already. The difference is huge.
Re:How to navigate directories faster with Bash (2015) by I_complete_me on Thursday April 22, 2021 @03:53AM (#26900015)
In zsh, you use -> % take <directory> which makes the dir and cd's into it.
Re:How to navigate directories faster with Bash (2015) by JNRowe on Thursday April 22, 2021 @09:46AM (#26902661)
That isn't standard zsh behaviour. I suspect it comes from oh-my-zsh¹, but wouldn't be surprised if it isn't a bunch of the configuration bundles.

¹ https://github.com/ohmyzsh/ohmyzsh/blob/master/lib/functions...

Re:How to navigate directories faster with Bash (2015) by Normille on Thursday April 22, 2021 @11:20AM (#26903731)
The bash version works in zsh too.
Re:How to navigate directories faster with Bash (2015) by henrik_w on Thursday April 22, 2021 @08:29AM (#26901966)
escpae-dot (.) also works for this: "It also works to use !$ instead of escape-dot, but that is slightly harder to type, so I don’t use that anymore."

Wrote about it here: https://henrikwarne.com/2018/08/11/my-favorite-command-line-...

Re:How to navigate directories faster with Bash (2015) by loulouxiv on Thursday April 22, 2021 @08:56AM (#26902190)
Which you can type as ALT-.
Re:How to navigate directories faster with Bash (2015) by yrro on Thursday April 22, 2021 @09:00AM (#26902243)
This is the `yank-last-arg` readline comment. Bound to M-. and M-_ by default.

"Insert last argument to the previous command (the last word of the previous history entry). With a numeric argument, behave exactly like yank-nth-arg. Successive calls to yank-last-arg move back through the history list, inserting the last word (or the word specified by the argument to the first call) of each line in turn."

https://tiswww.case.edu/php/chet/readline/readline.html#IDX9...

Its sibling is `yank-nth-arg`, bound to M-C-y. So if you want the 2nd argument from the last command, you can press M-1 followed by M-C-y (yeah, arguments are indexed from 0)...

https://tiswww.case.edu/php/chet/readline/readline.html#IDX9...

Re:How to navigate directories faster with Bash (2015) by joseluisq on Thursday April 22, 2021 @03:37AM (#26899886)
I got stuck with exactly that use case today and trying to figure out how I can enter directly to my dir without typing it again. Bash is no so neat at completions like Fish for example. However !$ does the thing and keeps my fingers healthy.

That's great set of tips BTW.

Re:How to navigate directories faster with Bash (2015) by llbeansandrice on Thursday April 22, 2021 @12:11PM (#26904425)
I know this is a bash thread but zsh/oh-my-zsh will replace !$ with the actual argument when you type a space so you can see and edit it before executing the command.
Re:How to navigate directories faster with Bash (2015) by JNRowe on Thursday April 22, 2021 @01:12PM (#26905194)
bash supports the same, also named magic-space. For bash you can chuck something like "Space: magic-space" in your $INPUTRC, although you'd probably want a guard(see Readline Conditional Constructs in the manpage).

FWIW, zsh without oh-my-zsh can be configured to do too this with "bindkey ' ' magic-space".

Re:How to navigate directories faster with Bash (2015) by rnestler on Thursday April 22, 2021 @03:10AM (#26899711)
I hava a mkcd alias to create a directory and changing into it.
Re:How to navigate directories faster with Bash (2015) by barrkel on Thursday April 22, 2021 @12:02PM (#26904302)
I have one called `enter`.

    enter () 
    { 
        mkdir -p "$1";
        cd "$1"
    }
Re:How to navigate directories faster with Bash (2015) by mlang23 on Thursday April 22, 2021 @09:46AM (#26902651)
Also take a look at M-. (Meta+dot) which does the same thing while interactively editing a command...
Re:How to navigate directories faster with Bash (2015) by unvs on Thursday April 22, 2021 @08:13AM (#26901831)
Yes, that's a good one! My way of remembering is "bang for my buck" -> !$
Re:How to navigate directories faster with Bash (2015) by myicgygc843467 on Thursday April 22, 2021 @04:56AM (#26900431)
Is !$ the same as $_ ?
Re:How to navigate directories faster with Bash (2015) by johncoltrane on Thursday April 22, 2021 @06:01AM (#26900855)
Re:How to navigate directories faster with Bash (2015) by tome on Thursday April 22, 2021 @07:47AM (#26901651)
Looks like !$ is a readline feature and $_ is a shell variable?
Re:How to navigate directories faster with Bash (2015) by yrro on Thursday April 22, 2021 @09:09AM (#26902323)
Re:How to navigate directories faster with Bash (2015) by mturmon on Thursday April 22, 2021 @12:03PM (#26904309)
I don't think !$ is properly understood as a readline feature. It's a sh/bash abbreviation.

!! -> last whole command

!!:1 -> first argument of last command

!!:$ -> last argument of last command

!$ -> abbreviation for above

Re:How to navigate directories faster with Bash (2015) by mfontani on Thursday April 22, 2021 @08:43AM (#26902078)
mkdir -p foo && cd $_