Questions tagged [history-expansion]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
2 votes
1 answer
87 views

fill in from previous command arguments

Can I make the right-arrow key fill in an argument-at-a-time from the previous command line? $ mogrify -resize 50% file.jpg $ pressing right arrow now should fill in mogrify, pressing right arrow ...
user avatar
1 vote
1 answer
37 views

Is there a way to match history entries on multiple words/tokens of the command, when performing history expansion?

I'm working with a command-line tool that provides a number of subcommands that all use the same binary, e.g. tool foo, tool bar, etc, and as I work, these commands are placed into my Bash shell ...
user avatar
  • 744
2 votes
0 answers
91 views

Zsh: Is it possible to disable history expansion inside double quotes?

Is it possible to disable history expansion inside double quotes? I do not want history expansion to be completely disabled, as I still use it, but I see no reason to expand the history inside a ...
user avatar
  • 1,287
5 votes
4 answers
427 views

bash - get 1st argument of current command I am editing via history, or similar? [duplicate]

Often I'm writing a command in a bash prompt, where I want to get previous arguments, in the CURRENT line I'm typing out, and put in other places in the command. A simple example, would be if I want ...
user avatar
  • 1,327
0 votes
0 answers
63 views

What does `echo ${!a}` do and why? [duplicate]

The following bash script prints out c, but I'm not sure why. a=b b=c echo ${!a} I believe that !a will be converted to the last command which began with a, so in this case that would be a=b. This ...
user avatar
0 votes
0 answers
20 views

history expansion without execution? [duplicate]

!924 will execute that line from history. Is there a syntax where I can simply pull that line from history to the current line so I can edit it: i.e.: like using the up arrow with a built in search ...
user avatar
  • 357
0 votes
1 answer
30 views

How to commit *nix command line history so it's available to other sessions?

What I have seen is, command line history from one session (pts) is not available to other open sessions (pts). Is it possible to explicitly commit the history of a session, so that it's available ...
user avatar
  • 554
0 votes
0 answers
28 views

history manipulation in tcsh, substitute every match

I know everybody will tell me that I should switch from using tcsh to bash or something else, but hysteresis is a powerful force, so I'm not going to. Most of the time I find tcsh perfectly ...
user avatar
  • 423
3 votes
1 answer
92 views

How to refer to a command beginning with $?

Assume that the history list contains the command e='echo a b c' and then $e. How to refer to the command $e using the history expansion feature of bash?
user avatar
  • 695
1 vote
2 answers
85 views

View History Expansion On History

I am having to to rewrite history expansion commands, instead of calling it from history. For Example, I have to change 35 to 36, 37, 38.... in the following command. $ print -P '\033[35mThis is the ...
user avatar
  • 1,201
-2 votes
1 answer
1k views

How can I use passwords with bash special characters on the command line? [duplicate]

I have a command line that takes a password . The password starts with characters '!1'. E.g. '!1x9y377s' . If I execute a command with that password, then history expansion occurs and the first two ...
user avatar
3 votes
2 answers
380 views

Escaping a '!' within double quotes

The bash manual entry for double quotes (https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html) states: If enabled, history expansion will be performed unless an ‘!’ appearing in ...
user avatar
  • 227
0 votes
0 answers
152 views

How does history print to the terminal?

EDIT: As per @JdeBP's comment, I've learned that the history command and the history recall functionality of interactive terminals are two separate things. I am, however, still curious about how the ...
user avatar
1 vote
0 answers
68 views

How to search special character in history expansion in bash or zsh?

Some characters such as ?, ^, $ have special meaning in history expansion, can I search them in the !string or !?string[?] event ?. The help says nothing about how to do it: `!STRING' Refer to ...
user avatar
  • 968
3 votes
1 answer
209 views

What is the use of `!#` in csh, bash, zsh and probably other shells?

From man bash: !# The entire command line typed so far. From man zshall: !# Refer to the current command line typed in so far. The line is treated as if it were complete up to and ...
user avatar
1 vote
1 answer
236 views

Setting up aliases for a history expansion pattern

similar to the existing $_ which I learned stands for !-1:$, I would like to create aliases for $__, $___ and so on which refer to the 2nd or 3rd -last command. I have tried adding alias "$__"='!-2:$'...
user avatar
  • 85
1 vote
1 answer
34 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) ...
user avatar
1 vote
1 answer
250 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 ...
user avatar
4 votes
1 answer
407 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 ...
user avatar
1 vote
1 answer
1k 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 ...
user avatar
1 vote
1 answer
445 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,...
user avatar
  • 1,287
2 votes
2 answers
574 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?
user avatar
  • 1,287
4 votes
1 answer
256 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))}' ...
user avatar
  • 1,287
1 vote
1 answer
52 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 ...
user avatar
  • 1,287
5 votes
1 answer
333 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 ...
user avatar
  • 3,530
0 votes
1 answer
141 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 "...
user avatar
2 votes
1 answer
945 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 ...
user avatar
  • 496
0 votes
2 answers
78 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:...
user avatar
1 vote
2 answers
775 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 ‘$’, ‘`’, ...
user avatar
  • 89.7k
0 votes
1 answer
1k 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 ...
user avatar
  • 2,281
2 votes
1 answer
100 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}
user avatar
25 votes
5 answers
7k 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 ...
user avatar
  • 611
20 votes
4 answers
3k 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 ...
user avatar
1 vote
2 answers
282 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 ...
user avatar
  • 111
1 vote
1 answer
328 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 ...
user avatar
6 votes
1 answer
283 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 ...
user avatar
  • 11.1k
0 votes
1 answer
113 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?
user avatar
2 votes
0 answers
279 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 ...
user avatar
3 votes
2 answers
754 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 !:...
user avatar
  • 251
-1 votes
1 answer
805 views

History substitution fails when implemented in shell script [duplicate]

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 /...
user avatar
  • 251
63 votes
2 answers
38k 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,...
user avatar
  • 3,513
2 votes
2 answers
3k 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 = ...
user avatar
  • 161
0 votes
2 answers
271 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' $...
user avatar
  • 292
14 votes
5 answers
16k 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 ...
user avatar
  • 244
2 votes
1 answer
420 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 ...
user avatar
  • 156
0 votes
1 answer
156 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 (...
user avatar
11 votes
2 answers
3k 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 "...
user avatar
4 votes
2 answers
6k 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?
user avatar
  • 11.3k
2 votes
2 answers
461 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 ...
user avatar
2 votes
1 answer
115 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 `!!:...
user avatar
  • 11.2k