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.

24
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,...
0
votes
1answer
18 views

Bash's history behavior when using ! and ! :p for old commands changed

Suddenly my bash history behavior changed. When I'm either calling an old command using !num (where num is the number of the commmand as seen in history) or displaying the command (without running it) ...
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 ...
2
votes
1answer
53 views

History expansion in scripts [duplicate]

I have the following Bash script case $- in (*H*) echo enabled ;; (*) echo disabled ;; esac set -H case $- in (*H*) echo enabled ;; (*) echo disabled ;; esac pwd last_command="!!" echo $last_command ...
0
votes
1answer
61 views

The -p option in the bash history command?

From man bash, history -p arg [arg ...] ... -p Perform history substitution on the following args and display the result on the standard output. What does 'history ...
20
votes
5answers
3k views

What does typing a single exclamation mark do in Bash?

Bash uses exclamation marks for history expansions, as explained in the answers to this question (e.g. sudo !! runs the previous command-line with sudo). However, I can't find anywhere that explains ...
1
vote
1answer
91 views

Non-greedy (extended) globs in zsh

I like to do non-greedy globs, but my Google searches hint that this is not supported. Is this the case? If so, why is it the case? For example I'd like to use a non-greedy glob in history expansions,...
2
votes
2answers
133 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?
2
votes
1answer
56 views

restricting hist_verify in zsh

Is it possible to bypass hist_verify for specific history commands? For instance, I rarely have the need to verify !$ or !!, whereas I find verification to be a useful feature for more complicated ...
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))}' ...
1
vote
1answer
31 views

How can I increase a number found by wildcard in the previous command? (zsh)

I want to accomplish this: setopt HIST_SUBST_PATTERN echo Ninja_Turtles_2003_S02E05_DVDRip_30NAMA.mkv ^E(0?)^E$((match[1]+1)) # resulting in: echo Ninja_Turtles_2003_S02E06_DVDRip_30NAMA.mkv ‌But I ...
0
votes
1answer
51 views

Multiple substitution when repeating the previous command

I know that I can simply substitute a string with another in the previous command by typing: !!:gs/string1/string2/ But how I can perform multiple substitutions, e.g. having a command: echo "...
0
votes
2answers
58 views

Should history expansion be quoted?

I've just come across !$ (without quotes). I've not met this before and did some tests: $ ls -l (...some output...) $ echo !$ -l $ echo "!$" -l man bash says this in the section on history expansion:...
0
votes
0answers
36 views

Wrap the history number - reset history number without losing history

I display the history number as part of my prompt, e.g., {!1123}(j:~)$ ls /some/long/and/hard/to/type/path so that I can do it later with !1123. My HISTSIZE and HISTFILESIZE are both 200, but this ...
1
vote
0answers
143 views

`history` command produces asterisk * entries

My modified search history lines have an asterisk next to them. I've searched unix.stackexchange.com and stackoverflow.com, but I yearn for a full explanation for the asterisks in my history (other ...
0
votes
1answer
197 views

When does history expansion happen in bash?

When does history expansion happen? From bash manual Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘`’, ...
0
votes
1answer
423 views

Why is the regex pattern '?!' not working with grep? [duplicate]

One could use -v to exclude a single word from a file, but I'm wondering why the regex pattern ?! is not working with grep/egrep. I was searching for a pattern to exclude a single word from my search ...
16
votes
4answers
2k views

Why is bash history substitution still enabled by default? [closed]

Does anybody know why bash still has history substitution enabled by default? My .bashrc has included set +H for many many years but some other people are still getting bitten by this feature. Given ...
2
votes
1answer
46 views

Can I use bash history modifiers with variables in scripts?

It would be handy if I could use bash history modifiers in scripts such as: !$:h to get the path of a file. Is there a way to use them in scripts? Eg ${1:h}
1
vote
2answers
105 views

Linux History Expansion escaping colon which is not mean as a modifier (in zsh)

I have a command like: echo test Now i want to use the last parameter inside another command with history extension inside a zsh. My other command have a leading colon ":" in it, so I want a result ...
2
votes
2answers
1k views

Echo & redirect bash history !commands to file

Is it possible to echo and redirect history commands into a script file by the ! command? Is there variation of the pipe, redirect, or tee commands I am overlooking? 518 xmodmap -e "keycode 66 = ...
1
vote
1answer
115 views

printing and not executing the result of zsh history expansion on partial search

I could do: !systemctl:p to get systemctl reload bind result printed (as last command in the history starting with systemctl string). but doing the same with the partial search on the command ...
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 ...
0
votes
1answer
91 views

After Oh-my-zsh install, how can you reenable direct launch of command with shebang?

After installing oh-my-zsh using !! and typing entry will expand the last command in a new prompt instead of directly relaunch it. How to configure zsh so it do launch directly the command?
2
votes
0answers
162 views

History expansion in fish

I want to run the previous command with part of it replaced. In bash, I can use the history expansion feature: ^x^y runs the previous command again, except with x replaced by y. I tried it in fish and ...
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?
3
votes
2answers
561 views

Sync two folders and on success copy one file from a location to the other

I am using rsync command to sync two folder and on success of rysnc I want to copy a file success and while copying append source folder name parameter like Success_FolderName.I am using $(basename !:...
-1
votes
1answer
405 views

History substitution fails when implemented in shell script

The below command works in command line rsync -avh -r /Source/09_03_2016/ /Destination/ echo $(basename !:3) Output 09_03_2016 But when I do this in shell script it does not work #! /bin/bash /...
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 ...
0
votes
2answers
184 views

How to write “!” symbol between double quotes in bash? [duplicate]

I can't figure out how to write ! symbol in bash scripts when putting it in double quotes strings. For example: var="hello! my name is $name! bye!" Something crazy happens: $ age=20 $ name='boda' $...
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 ...
1
vote
1answer
236 views

Why is bash expanding history/exclamation-mark when between single quotes

I am creating a script to configure a server from scratch, part of this is postgres. One of the issues I'm having is if a random password has an exclamation it seems to be expanded by bash: I want ...
0
votes
1answer
132 views

Persistent Bash history between detached processes

I haven't yet been able to find this among the Bash documentation so I was hoping it could get answered if I asked it here. Is there any way that I can, on execution of a script, branch its history (...
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 "...
2
votes
2answers
239 views

Livesearch with first 10 matches as preview for zsh-history?

I have seen a history-livesearch with a preview, that shows the 10 first matches in the history in a list below the BUFFER-line, which is updated on every keystroke, and acts like the standard ctrl+r ...
2
votes
1answer
87 views

Is using shorthand quick substitution of history expansion problematic?

In my terminal (bash 3), I sometimes use the quick substitution ^aa^bb^ ^string1^string2^ Quick substitution. Repeat the last command, replacing string1 with string2. Equivalent to `!!:...
5
votes
2answers
894 views

Why does !! inside an alias not work?

I have this alias set in my system /etc/bashrc file: alias root="sudo !!" The intention of this being to run the last used command using sudo of course. When used, it of course appears to substitute ...