Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

0 votes
1 answers
41 views
local zsh history on up-arrow, global zsh-history on ctrl-R
I am looking for a very specific behavior in my [Zsh](https://en.wikipedia.org/wiki/Z_shell) terminals. I have multiple terminal windows open at the same time. On each terminal window, if I hit the up arrow, I'd like for it to go up through the **local** history of that specific terminal window. On...
I am looking for a very specific behavior in my [Zsh](https://en.wikipedia.org/wiki/Z_shell) terminals. I have multiple terminal windows open at the same time. On each terminal window, if I hit the up arrow, I'd like for it to go up through the **local** history of that specific terminal window. On each terminal window, if I type Ctrl-R and then start typing to search through history, I would like it to search through the **global** history that comes from all the terminal windows combined. Is it possible? How can I do it?
mareoraft (649 rep)
Apr 23, 2025, 04:35 PM • Last activity: May 1, 2025, 07:59 AM
1 votes
2 answers
63 views
Using bash history expansion, can you replace the first argument of the last time a command ran, when it's not the most recent command?
The [GNU History Expansion reference][1] lacks examples, so I can't tell if this is possible. Here's what I want to do. I ran `cat long/path/to/file1.txt | less`, then I ran `vim file2.txt`. Using history expansion, I'd like to run `cat even/longer/path/to/file3.txt | less` but I don't want to: - Ty...
The GNU History Expansion reference lacks examples, so I can't tell if this is possible. Here's what I want to do. I ran cat long/path/to/file1.txt | less, then I ran vim file2.txt. Using history expansion, I'd like to run cat even/longer/path/to/file3.txt | less but I don't want to: - Type long/path/to/file1.txt, since it's inconvenient to type. - Refer to the previous command with a number; I want to refer to it with a string. i.e., Use !cat..., but not !-2... Can this be done? --- Here's what I've tried (doesn't work): !cat:^!cat:1^even/longer/path/to/file3.txt^ Here's my thought process: 1. !cat - Substitute the last *command* that began with the string cat. 1. :^x^y^ - Replace x with y in this command 1. !cat:1 - Expand to the first argument of the last cat command, i.e., long/path/to/file1.txt 1. even/longer/path/to/file3.txt – The replacement for that expanded text One obvious reason this doesn't work is I'm nesting expansions in expansions: !cat:1 is interpreted as a literal string. I get this output: long/path/to/file1.txtlong/path/to/file1.txteven/longer/path/to/file3.txt^: No such file or directory
Daniel Kaplan (1070 rep)
Apr 1, 2025, 06:58 PM • Last activity: Apr 2, 2025, 07:12 AM
4 votes
1 answers
134 views
zsh history expansion: Access second-last argument from previous command
Say I have executed a command foo a b c d e f I can then use `$!` to access the last argument of that command, i.e. if I type echo !$ it turns into `echo f`. Similarily I can use echo !:4 to access the 4th element, `d`, but it means that I have to count from left. Is there a notation to access the n...
Say I have executed a command foo a b c d e f I can then use $! to access the last argument of that command, i.e. if I type echo !$ it turns into echo f. Similarily I can use echo !:4 to access the 4th element, d, but it means that I have to count from left. Is there a notation to access the nth element from right?
user1934428 (767 rep)
Feb 13, 2025, 12:59 PM • Last activity: Feb 16, 2025, 10:09 PM
0 votes
1 answers
87 views
Using bash history expansion, how do I rerun the last command with a different last word?
*NOTE: I plan on answering my own question.* This is the last command I ran on the command line: git co head -- a/path/to/a/file.ts I want to run this a few more times but with different file paths for the last argument/parameter. This page explains [history expansion][1], and I think the answer I'm...
*NOTE: I plan on answering my own question.* This is the last command I ran on the command line: git co head -- a/path/to/a/file.ts I want to run this a few more times but with different file paths for the last argument/parameter. This page explains history expansion , and I think the answer I'm looking for is in this subsection , but it lacks examples. Using history expansion, how would I rerun the previous command with a different file path? For example, how would I run: git co head -- a/path/to/another/different-file.ts --- This is probably a duplicate but I haven't been able to find the answer I'm looking for. These posts look like they might have the answer but they didn't: - https://unix.stackexchange.com/questions/60781/rerunning-the-same-command-with-a-different-parameter - https://unix.stackexchange.com/questions/236506/replace-the-last-word-of-last-command-but-keep-the-first
Daniel Kaplan (1070 rep)
Sep 29, 2024, 08:54 PM • Last activity: Sep 29, 2024, 09:30 PM
5 votes
1 answers
362 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 string ... An example of my desired behavior: ``` $ echo "Hello!" Hello! $ !! Hello! ```
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 string ... An example of my desired behavior:
$ echo "Hello!"
Hello!
$ !!
Hello!
HappyFace (1694 rep)
Apr 10, 2021, 03:27 PM • Last activity: Sep 12, 2024, 06:29 AM
7 votes
2 answers
1117 views
Echoing "!" inside a string does some weird things
If I type in this: ```bash 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 that `!` is a **Special Variable** that you can use to reference your history. But as soon as I...
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 that ! is a **Special Variable** that you can use to reference your history. But as soon as I use this:
echo "Hello, World"!
I get my desired output. What is happening and why can't you use ! inside ""? --- Thanks for your help :)
Bog (1134 rep)
May 12, 2023, 02:14 PM • Last activity: May 16, 2023, 09:24 AM
0 votes
0 answers
214 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 % @@ ``` Result: ``` zsh: command not found: @@ ``` However, this works (note that `set` is omitted):...
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
% @@
Result:
zsh: command not found: @@
However, this works (note that set is omitted):
% histchars='@^#' 
% ls *.txt       
file1.txt  file2.txt
% @@
Result:
ls *.txt
file1.txt  file2.txt
I tried both with my .zshrc and with a vanilla shell (zsh --no-rcs), the result is the same. Question: is the set ... syntax wrong in the book, or am I missing something?
Attilio (385 rep)
Feb 23, 2023, 06:33 PM
0 votes
0 answers
475 views
History not working in bash
When I type at the bash prompt ```sh !5 ``` ... I simply get ```none !5: command not found ``` ... as the response. Any ideas about what I am doing wrong? Anything I should check?
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 (101 rep)
Jan 24, 2023, 05:06 AM • Last activity: Jan 24, 2023, 06:27 AM
64 votes
6 answers
48304 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 the double bang in the string itself, so that doesn't work. Is there any way to make this work? Or a...
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 the double bang in the string itself, so that doesn't work. Is there any way to make this work? Or an alternative alias? `
Manishearth (1007 rep)
Aug 2, 2013, 06:17 PM • Last activity: Dec 22, 2022, 06:14 PM
2 votes
1 answers
146 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" ``` ``` command not found: ^diff^add ```
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"
command not found: ^diff^add
gary (23 rep)
Oct 14, 2022, 06:50 AM • Last activity: Oct 14, 2022, 08:10 AM
72 votes
2 answers
45305 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, set to the absolute pathname used to invoke the shell or shell script being executed a...
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, set to the absolute pathname used to invoke the shell or shell script being executed as passed in the environment or argument list. Subsequently, expands to the **last argument to the previous command** after expansion. Also set to the full pathname used to invoke each command executed and placed in the environment exported to that command. There must be some difference I cannot catch, because: $ echo "hello" > /tmp/a.txt $ echo "!$" echo "/tmp/a.txt" /tmp/a.txt $ echo "hello" > /tmp/a.txt $ echo $_ hello What is the difference?
Loom (4073 rep)
Mar 23, 2016, 10:49 AM • Last activity: May 27, 2022, 12:51 PM
6 votes
1 answers
379 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 things like `echo`ing a multi-line string and pipe it through a script, but in some cases it gives an e...
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 things like echoing a multi-line string and pipe it through a script, but in some cases it gives an error: $ echo "foo bar" | sed '/foo/!d' bash: !d': event not found > The ! seems to trigger history expansion, although it is enclosed with single quotes. The problem seems to be the occurrence of the double quote in the same line, because $echo $'foo\nbar' | sed '/foo/!d' works as well as $echo "foo bar" | > sed '/foo/!d' My suspicion: History expansion is applied linewise, so the ' after a single " is considered to be escaped, so the following ! is not escaped. Now my question: Is this a bug or expected behavior? Reproduced with bash versions 4.2.30 and 4.4.12.
Philippos (13680 rep)
Sep 7, 2017, 12:23 PM • Last activity: Mar 17, 2022, 04:00 PM
2 votes
1 answers
230 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 again should fill in all the way up to `mogrify -resize`, if I were to press it again `mogrify -resize...
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 again should fill in all the way up to mogrify -resize, if I were to press it again mogrify -resize 50% etc. and in the case that the command line so far does not match the directly preceding command line, it should fill in from the most recent matching command $ mogrify -resize 50% file.jpg $ echo hi $ mo Pressing right arrow should fill in mogrify not echo. how can zsh be configured to support this?
user172877
Sep 24, 2021, 11:20 PM • Last activity: Sep 25, 2021, 10:15 AM
1 votes
1 answers
202 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 history, for example: ``` 7322 [2021-04-16 15:37:45 +0000] tool foo . 7323 [2021-04-16 15:37:47 +0000...
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 history, for example:
7322  [2021-04-16 15:37:45 +0000] tool foo .
 7323  [2021-04-16 15:37:47 +0000] tool bar
 7324  [2021-04-16 15:37:50 +0000] tool baz
 7325  [2021-04-16 15:38:01 +0000] tool qux -a -b asdf -c=100 /var/lib/foo/...
 7326  [2021-04-16 15:38:15 +0000] htop
 7327  [2021-04-16 15:38:21 +0000] tool foo . -x
The exact tool is proprietary (its exact functionality is irrelevant) and doesn't have specific features to help with tracking and recalling its own commands. For example, I'd like to recall command 7325, tool qux -a -b asdf -c=100 /var/lib/foo/... (suppose that it's not recent enough to just hit the up arrow a bunch of times). The commands and parameters vary often enough that establishing a Bash alias doesn't seem practical or convenient (either I edit .bashrc or I lose the alias when the shell closes). I'm pretty confident that the *last* time I ran tool qux, it had the correct parameters that I would want to use, or a reasonably safe set that I would need to edit anyway. I know I could run history | grep qux to look for the history index and then run !7325. Is there a way I can *directly* recall it with one set of keystrokes typed into the Bash prompt? !tool qux doesn't work because in this scenario it will run tool foo . -x qux instead. I tried quoting it, but it looks like expansion happens earlier.
nanofarad (743 rep)
Apr 16, 2021, 03:54 PM • Last activity: Apr 16, 2021, 04:33 PM
3 votes
1 answers
140 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?
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 (793 rep)
Jul 10, 2020, 11:59 AM • Last activity: Apr 2, 2021, 04:53 AM
5 votes
4 answers
1182 views
bash - get 1st argument of current command I am editing via history, or similar?
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 to rename a file. I would - type out the `mv` command - type the filename I want to move, `~/myTestFi...
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 to rename a file. I would - type out the mv command - type the filename I want to move, ~/myTestFileWithLongFilename.txt - now I want to just change the extension of the file that I supplied in the first argument, without typing it again. Can I use history or bash completion in some way to autocomplete that first argument? $ mv ~/myTestFileWithLongFilename.txt ~/myTestFileWithLongFilename.md I know of course I could execute the incomplete command, to get it into the history, and then reference it with !$, but then my history is polluted with invalid commands, and I'm wondering if there's a better way
Brad Parks (1769 rep)
Mar 24, 2021, 06:42 PM • Last activity: Mar 25, 2021, 05:52 PM
0 votes
0 answers
80 views
What does `echo ${!a}` do and why?
The following bash script prints out `c`, but I'm not sure why. ```bash 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 should mean that the last command is equivalent to the following. ```bash echo ${a=...
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 should mean that the last command is equivalent to the following.
echo ${a=b}
However, that prints b, not c. Is anyone able to explain why this is the case?
Abraham Murciano Benzadon (131 rep)
Mar 8, 2021, 03:36 PM
1 votes
2 answers
2377 views
When does history expansion happen in bash?
When does history expansion happen? 1. From bash manual > Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘`’, ‘\’, and, when history expansion is enabled, ‘!’. Since double quotes are recognized at parsing stage...
When does history expansion happen? 1. From bash manual > Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘`’, ‘\’, and, when history expansion is enabled, ‘!’. Since double quotes are recognized at parsing stage by the parser, is it correct that history expansion must happen after parsing? If yes, when does it happen with respect to shell expansions such as brace expansion, parameter expansion, filename expansion, etc? 2. But I think that history expansion is provided by the readline of the shell, so is processed before lexical analysis and parsing? Just like auto-completion in shell. Am I missing something? Thanks.
Tim (106420 rep)
May 20, 2018, 09:51 PM • Last activity: Feb 2, 2021, 12:12 PM
0 votes
0 answers
20 views
history expansion without execution?
!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 feature?
!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 feature?
kevcoder (515 rep)
Nov 15, 2020, 06:20 PM
6 votes
1 answers
3494 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 than what the man page says). > Lines listed with a * have been modified. Example: $ history | tail 11...
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 than what the man page says). > Lines listed with a * have been modified. Example: $ history | tail 11850* 11851 ./block_ip.sh '23.228.114.203' 'evil probe' 11852 ./block_ip.sh DROP '23.228.114.203' 'evil probe $ _________________ In this example, a shell script had a third argument, but there was no error, and i ran it twice without specifying (DROP/ACCEPT). The modification was an attempt to blank out this history so that history-expansion would not lead me to the wrong command (again). I want to know more about this (*but I don't know what I don't know*). Please consider both angles of this: - how can i use this (*for instance can i get that original command if i need it*)? - how can a bad guy use this (*can someone hide their command history this way*)? ____________________________________ If a generic answer is too verbose, please note some of my settings: EDITOR=/usr/bin/vim HISTFILE=/home/jim/.bash_history SHELLOPTS=braceexpand:hashall:histexpand:history:interactive-comments:monitor:vi And this OS info (*It is RedHat...but Debian/Fedora/Ubuntu shouldn't vary much...should they?*): Linux qwerutyhgfjkd 3.10.0-693.11.1.el7.x86_64 #1 SMP Mon Dec 4 23:52:40 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux I am using bash as my shell.
WEBjuju (536 rep)
Jun 6, 2018, 07:51 PM • Last activity: Oct 20, 2020, 06:48 AM
Showing page 1 of 20 total questions