0

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.

4
  • Did you source ~/.bashrc after you added it? Does type getlast show the exact function definition you posted? Commented Feb 26, 2014 at 7:05
  • yeah, i did that as always. Commented Feb 26, 2014 at 13:34
  • you either do function getlast {...} or getlast() {...} nothing else Commented Feb 26, 2014 at 13:57
  • OK. changed that and still not working... Commented Feb 26, 2014 at 14:06

2 Answers 2

1

try to add --posix and/or -e before the ' after sed

function getlast() {
    fc -ln "$1" "$2" | sed --posix -e '1s/^[[:space:]]*//'
}

under AIX (so not GNU sed and --posix not allowed)

bash-3.2$      echo "with space before\n"
with space before

bash-3.2$ getlast -1 -2

getlast -1 -2
+ getlast -1 -2
+ set -vx
+ fc -ln -1 -2
+ sed -e '1s/^[[:space:]]*//'
echo "with space before"
         getlast -1 -2
Sign up to request clarification or add additional context in comments.

2 Comments

I just add a session test, work here. What is your session error (including maybe a set -vx in your function like on my session)
OK. It works now. I added a new edit to explain what happened although I still don't quite understand it. Thanks for your answer and time!
0

bash is not magic, so if you're getting an error message from sed, and the code you think you are running has no sed involved, it's obviously not the code you are actually running. you can see the definition with

typeset -f getlast

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.