Reputation: 24824
how I can use the second argument of previous command in a new command ?
example, with
$ mkdir test
I make a directory, how I can use the name of directory for change to this ?
$ mkdir test && cd use_var
Upvotes: 2
Views: 845
Reputation: 532333
With history expansion, you can refer to arbitrary words in the current command line
mkdir dir1 && cd "!#:1"
# 0 1 2 3 4
!#
refers to the line typed so far, and :1
refers to word number one (with mkdir
starting at 0).
If you use this in a script (i.e., a non-interactive shell), you need to turn history expansion on with set -H
and set -o history
.
Upvotes: 5
Reputation: 328810
I use functions for this. Type this in your shell:
mkcd() { mkdir "$1" ; cd "$1" ; }
Now you have a new command mkcd
.
If you need this repeatedly, put the line into the file ~/.bash_aliases
(if you use bash
; other shells use different names).
Upvotes: 1
Reputation: 13717
Pressing Esc + . places the last argument of previous command on the current place of cursor. Tested in bash
shell and ksh
shell.
Upvotes: 2
Reputation: 44434
$_
is the last (right-most) argument of the previous command.
mkdir gash && cd "$_"
(I don't create files or directories called test
, that's the name of a shell built-in and can cause confusions)
Upvotes: 6