Skip to main content

Questions tagged [history-expansion]

The tag has no usage guidance.

7 votes
2 answers
1k views

Echoing "!" inside a string does some weird things [duplicate]

If I type in this: echo "Hello, World!" I don't know the name of it, but it prompts me for the next line. You know the PS2 thing. Or if you type echo \ and press Enter. Why? Well I know ...
Bog's user avatar
  • 1,034
0 votes
0 answers
148 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 % @@ ...
Attilio's user avatar
  • 365
0 votes
0 answers
248 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?
Kiwiheretic's user avatar
2 votes
1 answer
136 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&...
gary's user avatar
  • 23
2 votes
1 answer
217 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 ...
minseong's user avatar
  • 837
1 vote
1 answer
166 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 ...
nanofarad's user avatar
  • 743
4 votes
0 answers
268 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 ...
HappyFace's user avatar
  • 1,620
5 votes
4 answers
946 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 ...
Brad Parks's user avatar
  • 1,689
0 votes
0 answers
71 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 ...
Abraham Murciano Benzadon's 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 ...
kevcoder's user avatar
  • 485
0 votes
1 answer
337 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 ...
samshers's user avatar
  • 688
0 votes
0 answers
106 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 ...
Leo Simon's user avatar
  • 453
3 votes
1 answer
136 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?
tmpbin's user avatar
  • 783
1 vote
2 answers
218 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 ...
Ahmad Ismail's user avatar
  • 2,738
-2 votes
1 answer
5k 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 ...
Brent Fisher's user avatar
4 votes
2 answers
834 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 ...
Adam's user avatar
  • 257
0 votes
0 answers
244 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 ...
ExecutionByFork's user avatar
1 vote
0 answers
106 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 ...
dedowsdi's user avatar
  • 1,018
3 votes
1 answer
301 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 ...
yukashima huksay's user avatar
1 vote
1 answer
443 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:$'...
siryx's user avatar
  • 95
1 vote
1 answer
43 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) ...
Cedric Martin's user avatar
1 vote
1 answer
424 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 ...
History_expansion's user avatar
4 votes
1 answer
772 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 ...
History_expansion's user avatar
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 ...
Elliptical view's user avatar
1 vote
1 answer
766 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,...
HappyFace's user avatar
  • 1,620
2 votes
2 answers
926 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?
HappyFace's user avatar
  • 1,620
4 votes
1 answer
363 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))}' ...
HappyFace's user avatar
  • 1,620
1 vote
1 answer
63 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 ...
HappyFace's user avatar
  • 1,620
5 votes
1 answer
654 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 ...
user001's user avatar
  • 3,738
0 votes
1 answer
233 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 "...
K. Koovalsky's user avatar
3 votes
1 answer
3k 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 ...
WEBjuju's user avatar
  • 506
0 votes
2 answers
100 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 ‘$’, ‘`’, ...
Tim's user avatar
  • 103k
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 ...
manifestor's user avatar
  • 2,483
2 votes
1 answer
130 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}
user1371264's user avatar
25 votes
5 answers
9k 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 ...
ash's user avatar
  • 680
21 votes
4 answers
4k 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 ...
Mikko Rantalainen's user avatar
1 vote
2 answers
371 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 ...
snap's user avatar
  • 111
1 vote
1 answer
371 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
352 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 ...
Philippos's user avatar
  • 13.5k
0 votes
1 answer
140 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?
psychoslave's user avatar
3 votes
0 answers
443 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 ...
Crunchy234's user avatar
3 votes
2 answers
936 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 !:...
HDev007's user avatar
  • 261
-1 votes
1 answer
879 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 /...
HDev007's user avatar
  • 261
70 votes
2 answers
44k 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,...
Loom's user avatar
  • 3,963
2 votes
2 answers
5k 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 = ...
phillipsk's user avatar
  • 161
1 vote
2 answers
310 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' $...
bodacydo's user avatar
  • 322
15 votes
5 answers
18k 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 ...
Arturas M's user avatar
  • 254
2 votes
1 answer
544 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 ...
ZZ9's user avatar
  • 156
0 votes
1 answer
194 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 (...
Francesco Gramano's user avatar

15 30 50 per page