Run command from history with parameters
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
Whenever I need to type a long command I find that the exclamation mark to refer to history very handy. For example, !ssh
can be used to invoke the last ssh
command I used. However I cannot get it to work when space-delimited arguments are added to the search.
By executing !ssh -i
, I expect to the last command starting with the string "ssh -i" to be executed. However, the actual command that is invoked is the last ssh
command with whatever argument, and then "-i" was appended to the command.
I can use !?ssh -i?
but that is meant to do something different and I believe there should be a simple way to achieve this. I am also reluctant to type history|grep ...
every time.
So how can I refer to a command in history that starts with a string containing space, as stated above? Any help will be appreciated.
Edit It seems that I have not explained the question clear enough. Let's say I have the following history
ssh -i identity host1
echo ssh -i
ssh host2
now if I run !ssh
, the third command, namely ssh host2
will be executed. I expect to type !ssh -i
to get the first command executed, but ssh host2 -i
is executed instead (which results in an error). I can use !?ssh -i?
to execute the first command only if the second command doesn't exist.
I hope this helps clarify my question.
bash command-history csh
 |Â
show 3 more comments
up vote
0
down vote
favorite
Whenever I need to type a long command I find that the exclamation mark to refer to history very handy. For example, !ssh
can be used to invoke the last ssh
command I used. However I cannot get it to work when space-delimited arguments are added to the search.
By executing !ssh -i
, I expect to the last command starting with the string "ssh -i" to be executed. However, the actual command that is invoked is the last ssh
command with whatever argument, and then "-i" was appended to the command.
I can use !?ssh -i?
but that is meant to do something different and I believe there should be a simple way to achieve this. I am also reluctant to type history|grep ...
every time.
So how can I refer to a command in history that starts with a string containing space, as stated above? Any help will be appreciated.
Edit It seems that I have not explained the question clear enough. Let's say I have the following history
ssh -i identity host1
echo ssh -i
ssh host2
now if I run !ssh
, the third command, namely ssh host2
will be executed. I expect to type !ssh -i
to get the first command executed, but ssh host2 -i
is executed instead (which results in an error). I can use !?ssh -i?
to execute the first command only if the second command doesn't exist.
I hope this helps clarify my question.
bash command-history csh
1
Why not use ctrl-Rssh -i
instead? I never use history expansion, but always history search, because (a) it always works the same way and (b) it keeps me from mistakes because I can see the line to execute before I really do it and (c) I can doset +H
by default, so the bad implementation ofbash
history expansion won't mess up my scripts.
â Philippos
Jan 5 at 16:20
Thank you for your suggestion. It makes sense to me. I am just not yet used to Ctrl-R. I will consider switching to it. For the "never use history expansion" part, history expansion is useful if you want to embed it into a larger command.
â Weijun Zhou
Jan 5 at 16:25
@Philippos Just noticed that Ctrl-R takes me to the second command, not the first.
â Weijun Zhou
Jan 5 at 16:35
Come on! You just invented theecho ssh -i
line to exclude using Ctrl-R!
â Philippos
Jan 5 at 16:36
Sorry about it. I just realized that when you posted that comment, my edit has not even started.
â Weijun Zhou
Jan 5 at 16:41
 |Â
show 3 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Whenever I need to type a long command I find that the exclamation mark to refer to history very handy. For example, !ssh
can be used to invoke the last ssh
command I used. However I cannot get it to work when space-delimited arguments are added to the search.
By executing !ssh -i
, I expect to the last command starting with the string "ssh -i" to be executed. However, the actual command that is invoked is the last ssh
command with whatever argument, and then "-i" was appended to the command.
I can use !?ssh -i?
but that is meant to do something different and I believe there should be a simple way to achieve this. I am also reluctant to type history|grep ...
every time.
So how can I refer to a command in history that starts with a string containing space, as stated above? Any help will be appreciated.
Edit It seems that I have not explained the question clear enough. Let's say I have the following history
ssh -i identity host1
echo ssh -i
ssh host2
now if I run !ssh
, the third command, namely ssh host2
will be executed. I expect to type !ssh -i
to get the first command executed, but ssh host2 -i
is executed instead (which results in an error). I can use !?ssh -i?
to execute the first command only if the second command doesn't exist.
I hope this helps clarify my question.
bash command-history csh
Whenever I need to type a long command I find that the exclamation mark to refer to history very handy. For example, !ssh
can be used to invoke the last ssh
command I used. However I cannot get it to work when space-delimited arguments are added to the search.
By executing !ssh -i
, I expect to the last command starting with the string "ssh -i" to be executed. However, the actual command that is invoked is the last ssh
command with whatever argument, and then "-i" was appended to the command.
I can use !?ssh -i?
but that is meant to do something different and I believe there should be a simple way to achieve this. I am also reluctant to type history|grep ...
every time.
So how can I refer to a command in history that starts with a string containing space, as stated above? Any help will be appreciated.
Edit It seems that I have not explained the question clear enough. Let's say I have the following history
ssh -i identity host1
echo ssh -i
ssh host2
now if I run !ssh
, the third command, namely ssh host2
will be executed. I expect to type !ssh -i
to get the first command executed, but ssh host2 -i
is executed instead (which results in an error). I can use !?ssh -i?
to execute the first command only if the second command doesn't exist.
I hope this helps clarify my question.
bash command-history csh
edited Jan 5 at 16:21
asked Jan 5 at 16:03
Weijun Zhou
1,434119
1,434119
1
Why not use ctrl-Rssh -i
instead? I never use history expansion, but always history search, because (a) it always works the same way and (b) it keeps me from mistakes because I can see the line to execute before I really do it and (c) I can doset +H
by default, so the bad implementation ofbash
history expansion won't mess up my scripts.
â Philippos
Jan 5 at 16:20
Thank you for your suggestion. It makes sense to me. I am just not yet used to Ctrl-R. I will consider switching to it. For the "never use history expansion" part, history expansion is useful if you want to embed it into a larger command.
â Weijun Zhou
Jan 5 at 16:25
@Philippos Just noticed that Ctrl-R takes me to the second command, not the first.
â Weijun Zhou
Jan 5 at 16:35
Come on! You just invented theecho ssh -i
line to exclude using Ctrl-R!
â Philippos
Jan 5 at 16:36
Sorry about it. I just realized that when you posted that comment, my edit has not even started.
â Weijun Zhou
Jan 5 at 16:41
 |Â
show 3 more comments
1
Why not use ctrl-Rssh -i
instead? I never use history expansion, but always history search, because (a) it always works the same way and (b) it keeps me from mistakes because I can see the line to execute before I really do it and (c) I can doset +H
by default, so the bad implementation ofbash
history expansion won't mess up my scripts.
â Philippos
Jan 5 at 16:20
Thank you for your suggestion. It makes sense to me. I am just not yet used to Ctrl-R. I will consider switching to it. For the "never use history expansion" part, history expansion is useful if you want to embed it into a larger command.
â Weijun Zhou
Jan 5 at 16:25
@Philippos Just noticed that Ctrl-R takes me to the second command, not the first.
â Weijun Zhou
Jan 5 at 16:35
Come on! You just invented theecho ssh -i
line to exclude using Ctrl-R!
â Philippos
Jan 5 at 16:36
Sorry about it. I just realized that when you posted that comment, my edit has not even started.
â Weijun Zhou
Jan 5 at 16:41
1
1
Why not use ctrl-R
ssh -i
instead? I never use history expansion, but always history search, because (a) it always works the same way and (b) it keeps me from mistakes because I can see the line to execute before I really do it and (c) I can do set +H
by default, so the bad implementation of bash
history expansion won't mess up my scripts.â Philippos
Jan 5 at 16:20
Why not use ctrl-R
ssh -i
instead? I never use history expansion, but always history search, because (a) it always works the same way and (b) it keeps me from mistakes because I can see the line to execute before I really do it and (c) I can do set +H
by default, so the bad implementation of bash
history expansion won't mess up my scripts.â Philippos
Jan 5 at 16:20
Thank you for your suggestion. It makes sense to me. I am just not yet used to Ctrl-R. I will consider switching to it. For the "never use history expansion" part, history expansion is useful if you want to embed it into a larger command.
â Weijun Zhou
Jan 5 at 16:25
Thank you for your suggestion. It makes sense to me. I am just not yet used to Ctrl-R. I will consider switching to it. For the "never use history expansion" part, history expansion is useful if you want to embed it into a larger command.
â Weijun Zhou
Jan 5 at 16:25
@Philippos Just noticed that Ctrl-R takes me to the second command, not the first.
â Weijun Zhou
Jan 5 at 16:35
@Philippos Just noticed that Ctrl-R takes me to the second command, not the first.
â Weijun Zhou
Jan 5 at 16:35
Come on! You just invented the
echo ssh -i
line to exclude using Ctrl-R!â Philippos
Jan 5 at 16:36
Come on! You just invented the
echo ssh -i
line to exclude using Ctrl-R!â Philippos
Jan 5 at 16:36
Sorry about it. I just realized that when you posted that comment, my edit has not even started.
â Weijun Zhou
Jan 5 at 16:41
Sorry about it. I just realized that when you posted that comment, my edit has not even started.
â Weijun Zhou
Jan 5 at 16:41
 |Â
show 3 more comments
2 Answers
2
active
oldest
votes
up vote
1
down vote
I believe you want to change the default behavior of bash's HISTCONTROL.
From this post:
Why is bash not storing commands that start with spaces?
HISTCONTROL
A colon-separated list of values controlling how commands are saved on the history list. If the list of values includes ignorespace, lines which begin with a space character are not saved in the history list. A value of ignoredups causes lines matching the previous history entry to not be saved. A value of ignoreboth is shorthand for ignorespace and ignoredups.
If you want to skip the ! and allow for spaces then I suggest you try a shell other than bash. Fish shell works perfectly for what you're asking :)
sudo apt-get install fish
to try it from your bash shell just type, 'fish'
If you like it you can persist it with:
chsh -s /usr/bin/fish
Thank you for your answer, but I am referring to spaces in the middle of the command, not the spaces at the beginning of the command.
â Weijun Zhou
Jan 5 at 16:16
add a comment |Â
up vote
0
down vote
accepted
Thanks to the comment by Mark Plotnick. I have found a solution. Although it is not using bash history expansion. (I believe using bash history expansion to achieve this is impossible after reading [section 9.3.1 of bash reference manual].(https://www.gnu.org/software/bash/manual/html_node/Event-Designators.html)
The solution is to put the following lines in ~/.inputrc
, or uncomment the corresponding lines in /etc/inputrc
for global effect.
"e[5~": history-search-backward
"e[6~": history-search-forward
The corresponding code can be found by pressing CtrlV and then the PageUp key, and the commands are taken from section 8.4.2 of bash manual.
After that I can use PageUp key to go to the first command as Mark commented.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
I believe you want to change the default behavior of bash's HISTCONTROL.
From this post:
Why is bash not storing commands that start with spaces?
HISTCONTROL
A colon-separated list of values controlling how commands are saved on the history list. If the list of values includes ignorespace, lines which begin with a space character are not saved in the history list. A value of ignoredups causes lines matching the previous history entry to not be saved. A value of ignoreboth is shorthand for ignorespace and ignoredups.
If you want to skip the ! and allow for spaces then I suggest you try a shell other than bash. Fish shell works perfectly for what you're asking :)
sudo apt-get install fish
to try it from your bash shell just type, 'fish'
If you like it you can persist it with:
chsh -s /usr/bin/fish
Thank you for your answer, but I am referring to spaces in the middle of the command, not the spaces at the beginning of the command.
â Weijun Zhou
Jan 5 at 16:16
add a comment |Â
up vote
1
down vote
I believe you want to change the default behavior of bash's HISTCONTROL.
From this post:
Why is bash not storing commands that start with spaces?
HISTCONTROL
A colon-separated list of values controlling how commands are saved on the history list. If the list of values includes ignorespace, lines which begin with a space character are not saved in the history list. A value of ignoredups causes lines matching the previous history entry to not be saved. A value of ignoreboth is shorthand for ignorespace and ignoredups.
If you want to skip the ! and allow for spaces then I suggest you try a shell other than bash. Fish shell works perfectly for what you're asking :)
sudo apt-get install fish
to try it from your bash shell just type, 'fish'
If you like it you can persist it with:
chsh -s /usr/bin/fish
Thank you for your answer, but I am referring to spaces in the middle of the command, not the spaces at the beginning of the command.
â Weijun Zhou
Jan 5 at 16:16
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I believe you want to change the default behavior of bash's HISTCONTROL.
From this post:
Why is bash not storing commands that start with spaces?
HISTCONTROL
A colon-separated list of values controlling how commands are saved on the history list. If the list of values includes ignorespace, lines which begin with a space character are not saved in the history list. A value of ignoredups causes lines matching the previous history entry to not be saved. A value of ignoreboth is shorthand for ignorespace and ignoredups.
If you want to skip the ! and allow for spaces then I suggest you try a shell other than bash. Fish shell works perfectly for what you're asking :)
sudo apt-get install fish
to try it from your bash shell just type, 'fish'
If you like it you can persist it with:
chsh -s /usr/bin/fish
I believe you want to change the default behavior of bash's HISTCONTROL.
From this post:
Why is bash not storing commands that start with spaces?
HISTCONTROL
A colon-separated list of values controlling how commands are saved on the history list. If the list of values includes ignorespace, lines which begin with a space character are not saved in the history list. A value of ignoredups causes lines matching the previous history entry to not be saved. A value of ignoreboth is shorthand for ignorespace and ignoredups.
If you want to skip the ! and allow for spaces then I suggest you try a shell other than bash. Fish shell works perfectly for what you're asking :)
sudo apt-get install fish
to try it from your bash shell just type, 'fish'
If you like it you can persist it with:
chsh -s /usr/bin/fish
edited Jan 5 at 20:22
answered Jan 5 at 16:14
David Aubin
438
438
Thank you for your answer, but I am referring to spaces in the middle of the command, not the spaces at the beginning of the command.
â Weijun Zhou
Jan 5 at 16:16
add a comment |Â
Thank you for your answer, but I am referring to spaces in the middle of the command, not the spaces at the beginning of the command.
â Weijun Zhou
Jan 5 at 16:16
Thank you for your answer, but I am referring to spaces in the middle of the command, not the spaces at the beginning of the command.
â Weijun Zhou
Jan 5 at 16:16
Thank you for your answer, but I am referring to spaces in the middle of the command, not the spaces at the beginning of the command.
â Weijun Zhou
Jan 5 at 16:16
add a comment |Â
up vote
0
down vote
accepted
Thanks to the comment by Mark Plotnick. I have found a solution. Although it is not using bash history expansion. (I believe using bash history expansion to achieve this is impossible after reading [section 9.3.1 of bash reference manual].(https://www.gnu.org/software/bash/manual/html_node/Event-Designators.html)
The solution is to put the following lines in ~/.inputrc
, or uncomment the corresponding lines in /etc/inputrc
for global effect.
"e[5~": history-search-backward
"e[6~": history-search-forward
The corresponding code can be found by pressing CtrlV and then the PageUp key, and the commands are taken from section 8.4.2 of bash manual.
After that I can use PageUp key to go to the first command as Mark commented.
add a comment |Â
up vote
0
down vote
accepted
Thanks to the comment by Mark Plotnick. I have found a solution. Although it is not using bash history expansion. (I believe using bash history expansion to achieve this is impossible after reading [section 9.3.1 of bash reference manual].(https://www.gnu.org/software/bash/manual/html_node/Event-Designators.html)
The solution is to put the following lines in ~/.inputrc
, or uncomment the corresponding lines in /etc/inputrc
for global effect.
"e[5~": history-search-backward
"e[6~": history-search-forward
The corresponding code can be found by pressing CtrlV and then the PageUp key, and the commands are taken from section 8.4.2 of bash manual.
After that I can use PageUp key to go to the first command as Mark commented.
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Thanks to the comment by Mark Plotnick. I have found a solution. Although it is not using bash history expansion. (I believe using bash history expansion to achieve this is impossible after reading [section 9.3.1 of bash reference manual].(https://www.gnu.org/software/bash/manual/html_node/Event-Designators.html)
The solution is to put the following lines in ~/.inputrc
, or uncomment the corresponding lines in /etc/inputrc
for global effect.
"e[5~": history-search-backward
"e[6~": history-search-forward
The corresponding code can be found by pressing CtrlV and then the PageUp key, and the commands are taken from section 8.4.2 of bash manual.
After that I can use PageUp key to go to the first command as Mark commented.
Thanks to the comment by Mark Plotnick. I have found a solution. Although it is not using bash history expansion. (I believe using bash history expansion to achieve this is impossible after reading [section 9.3.1 of bash reference manual].(https://www.gnu.org/software/bash/manual/html_node/Event-Designators.html)
The solution is to put the following lines in ~/.inputrc
, or uncomment the corresponding lines in /etc/inputrc
for global effect.
"e[5~": history-search-backward
"e[6~": history-search-forward
The corresponding code can be found by pressing CtrlV and then the PageUp key, and the commands are taken from section 8.4.2 of bash manual.
After that I can use PageUp key to go to the first command as Mark commented.
answered Jan 8 at 10:51
Weijun Zhou
1,434119
1,434119
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f415020%2frun-command-from-history-with-parameters%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Why not use ctrl-R
ssh -i
instead? I never use history expansion, but always history search, because (a) it always works the same way and (b) it keeps me from mistakes because I can see the line to execute before I really do it and (c) I can doset +H
by default, so the bad implementation ofbash
history expansion won't mess up my scripts.â Philippos
Jan 5 at 16:20
Thank you for your suggestion. It makes sense to me. I am just not yet used to Ctrl-R. I will consider switching to it. For the "never use history expansion" part, history expansion is useful if you want to embed it into a larger command.
â Weijun Zhou
Jan 5 at 16:25
@Philippos Just noticed that Ctrl-R takes me to the second command, not the first.
â Weijun Zhou
Jan 5 at 16:35
Come on! You just invented the
echo ssh -i
line to exclude using Ctrl-R!â Philippos
Jan 5 at 16:36
Sorry about it. I just realized that when you posted that comment, my edit has not even started.
â Weijun Zhou
Jan 5 at 16:41