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 of:

echo test:someothertext

And I try it with linux bash history extension like the following:

echo !!$:someothertext

The :s will be interpret as a modifier and the result is:

echo testsomeothertext

So, the needed colon is missing.

Real scenario is more useful and uses git commands (git show branch:file), but this echo example is easier to reproduce on each system.

I already have tried to use "\" as an escaping for the colon which don't work ("\" is printed in the result too) And I have tried to use a valid modifier so that the second colon would not be interpret but it also don't work.

So I tried this:

echo !!$:p:someothertext

which results in:

zsh: substitution failed
  • 1
    The tag says "bash" but the error message "zsh". – Hauke Laging Jan 10 '18 at 8:50
  • hi @HaukeLaging, yes you are right. I adjusted it. Thank you. – snap Jan 10 '18 at 8:56

Not really nice but in bash this works:

echo !!$\
  • hmmm i would like to put it in as a one liner maybe with other parameters expansions in it. – snap Jan 11 '18 at 14:40

You can always use:

echo !!$':moretext'


echo !!$'':moretext


echo $_:moretext
  • hmmm i don't want the apostrophe in the executed command. for echo it may work but in my more complex use case of "git show branch:file" (where i will replace the branch name) the apostrophe is a problem. the echo $_ works on echo. But if i use f.e. the following command as the second command git show $_:filename i get fatal: Invalid object name 'test'. – snap Jan 11 '18 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.