I tried to add a custom bash function in .bashrc file after reading this post: https://unix.stackexchange.com/questions/38072/how-can-i-save-the-last-command-to-a-file
I basically put
function getlast {
fc -ln "$1" "$2" | sed '1s/^[[:space:]]*//'
}
inside .bashrc.
Notice that I've changed the command line from
fc -ln "$1" "$1"
to
fc -ln "$1" "$2"
So I can have more flexibility on using the fc command.
Now just
getlast
worked fine as it gave the last command I just typed.
However, when I tried to use it with two command line arguments like this:
getlast -1 -3
I got this error message which is basically sed help message:
sed: invalid option -- '1'
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
-n, --quiet, --silent
suppress automatic printing of pattern space
-e script, --expression=script
add the script to the commands to be executed
-f script-file, --file=script-file
add the contents of script-file to the commands to be executed
--follow-symlinks
follow symlinks when processing in place
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
......
When i just use fc -ln like this
fc -ln -1 -3
It worked fine... and it also works fine on the command line to have sed after fc. So I know it's probably something with bash...
I also tried to twist sed by removing 1 inside ' ' and other things but it didn't work. I don't understand why it doesn't work...Can someone help me understand the problem here?
Thanks.
[EDIT]
I tried to remove the sed part so in the function, there is just:
function getlast {
fc -ln "$1" "$2"
}
And now getlast still works and getlast -1 -3 still gave me that same error message even though there is no sed involved... so the problem might be something else? Really confused...
[EDIT2]
It works now. I'm not sure I understand what exactly was going on.
Basically, after reading NeronLeVelu's answer and trying so many things, I realized it might be the particular bash session that have this problem. So I opened a new window within tmux and tried getlast -1 -2 and now it works... no more sed error...
Not sure what happened in the session I was working on.. but now it works.
source ~/.bashrcafter you added it? Doestype getlastshow the exact function definition you posted?function getlast {...}orgetlast() {...}nothing else