Questions tagged [history-expansion]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0 votes
0 answers
28 views

set histchars not working in zsh

Consider the following commands for overriding the history expansion characters (which I got from Learning Shell Scripting with ZSH book): % set histchars='@^#' % ls *.txt file1.txt file2.txt % @@ ...
  • 335
0 votes
0 answers
38 views

History not working in bash

When I type at the bash prompt !5 ... I simply get !5: command not found ... as the response. Any ideas about what I am doing wrong? Anything I should check?
2 votes
1 answer
112 views

How to create alias with a caret^ command?

I have a workflow that first check git diff for specific file and then add it to stage. git diff .. ^diff^add I want to give these command a alias but this one doesn't work alias da="^diff^add&...
  • 23
2 votes
1 answer
153 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 ...
1 vote
1 answer
88 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 ...
  • 734
3 votes
0 answers
171 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 ...
  • 1,423
5 votes
4 answers
618 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 ...
  • 1,579
0 votes
0 answers
68 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 ...
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 ...
  • 415
0 votes
1 answer
117 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 ...
  • 618
0 votes
0 answers
67 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 ...
  • 443
3 votes
1 answer
119 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?
  • 713
1 vote
2 answers
137 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 ...
  • 1,986
-2 votes
1 answer
3k 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 ...
3 votes
2 answers
568 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 ...
  • 237
0 votes
0 answers
192 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 ...
1 vote
0 answers
85 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 ...
  • 998
3 votes
1 answer
247 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 ...
1 vote
1 answer
379 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:$'...
  • 95
1 vote
1 answer
37 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) ...
1 vote
1 answer
333 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 ...
4 votes
1 answer
563 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 ...
2 votes
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 ...
1 vote
1 answer
591 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,...
  • 1,423
2 votes
2 answers
779 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?
  • 1,423
4 votes
1 answer
304 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,423
1 vote
1 answer
56 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 ...
  • 1,423
5 votes
1 answer
420 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,570
0 votes
1 answer
194 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 "...
2 votes
1 answer
1k 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 ...
  • 496
0 votes
2 answers
88 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
2k 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 ‘$’, ‘`’, ...
  • 95.9k
0 votes
1 answer
2k 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 ...
  • 2,403
2 votes
1 answer
115 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}
25 votes
5 answers
8k 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 ...
  • 631
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 ...
1 vote
2 answers
322 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 ...
  • 111
1 vote
1 answer
355 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
319 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 ...
  • 12.7k
0 votes
1 answer
119 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
0 answers
344 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
2 answers
857 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 !:...
  • 261
-1 votes
1 answer
852 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 /...
  • 261
66 votes
2 answers
41k 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,...
  • 3,753
2 votes
2 answers
4k 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 = ...
  • 161
1 vote
2 answers
299 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' $...
  • 312
14 votes
5 answers
17k 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 ...
  • 244
2 votes
1 answer
494 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 ...
  • 156
0 votes
1 answer
181 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
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 "...