Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [history-expansion]

The tag has no usage guidance.

47
votes
5answers
24k views

How can I `alias sudo !!`?

I'm trying to set an alias for sudo !! in Bash. I tried alias sbb='sudo !!', but it interprets that as a literal !! and prints sudo: !!: command not found If I use double quotes, it substitutes ...
25
votes
1answer
12k views

$_ vs !$. Last argument of the preceding command and output redirection

The question is about special variables. Documentation says: !!:$ designates the last argument of the preceding command. This may be shortened to !$. ($_, an underscore.) At shell startup,...
2
votes
1answer
170 views

Bash: History expansion inside single quotes after a double quote inside the same line

I took a closer look on this phenomenon after I stumbled over it in two other questions today. I've tried all of this with the default set -H (history expansion on). To test a script, I often do ...
3
votes
2answers
4k views

How do I get command name of the last executed command?

Example: I type man ls, than I want to get man only. By using !! I can get man ls but how do I get man?
11
votes
2answers
2k views

How to access the second argument from the last command in the history ?

I am starting to learn some Regex, therefore I use this command repeatedly: grep pattern /usr/share/dict/american-english Only the part with pattern changes, so I have to write the long expression "...
12
votes
5answers
12k views

How to recall a previous command (without execution) in order to change it?

I can't remember the trick where I could get the last command without running it: let's say I want to be be able to access the command !1255 when pressing the up arrow key and modify the command. So ...
0
votes
1answer
58 views

Using interactive's shell history expansion inside a script

I have the following Bash script set -o histexpand set -o history pwd lc="!!" which, when I run it in an interactive shell, prints /home/user lc="pwd" I'd like to, instead of getting lc=pwd, get ...
3
votes
1answer
67 views

How can I alias a history expansion in zsh?

I want this to work (it needs extendedglob and histsubstpattern): alias ri='^(#b)E(?)^E${(l:2::0:)$((match[1]+1))}' But it doesn't: $ alias sss='^(#b)E(?)^E${(l:2::0:)$((match[1]+1))}' ...
2
votes
2answers
130 views

Alternative of bash's `history -p` in zsh?

In bash, history -p does history expansion on its argument; What is the alternative in zsh?