0

This question is not a duplicate of Why alias behave different than running bash command directly? because I have tried that solution and it hasn't worked. I replaced all my single quotes with double quotes and vice-versa. The command works neither way.

I am trying to implement an alias, sys, that will open a file I keep track of changes to my system in, sys.md, append a date, yank that line, paste the line, replace it with -'s for the markdown h2 format, open a new line, and insert a - for the markdown bullet format. From vi.stackexchange.com I received some help implementing the command. It works:

nvim +'$pu_|r!date' +'norm yypVr-o ' ~/notes/sys.md

When I replace the single quotes with double quotes the $pu_ is evaluated and I get

zsh: event not found: date

So the single quotes are important. However, when I implement the alias

alias sys="nvim +'$pu_|r!date' +'norm yypVr-o- ' ~/notes/sys.md"

The alias fails. nvim opens , but the date is appended to the second line and not the bottom. Clearly $pu_ is being evaluated as a variable from BASH. (nvim's syntax checker even shows it to be evaluated.) Inverting the doubles and singles to yield

alias sys='nvim +"$pu_|r!date" +"norm yypVr-o- " ~/notes/sys.md'

Has the same behavior. Backticks,

alias sys=`nvim +"$pu_|r!date" +"norm yypVr-o- " ~/notes/sys.md`

Freezes bash on source .zshrc. I don't know how to proceed. Any help would be greatly appreciated.

  • Does escaping the $ in $pu_ not work? – Rakesh Sharma Aug 25 '18 at 17:36
  • To be honest, I didn't think of that. – malan Aug 25 '18 at 18:13
1

Just put it into a bash function (you can add the function to .bashrc):

sys() {
    nvim +'$pu_|r!date' +'norm yypVr-o ' ~/notes/sys.md
}

Then you can invoke it just like you would do this with an alias.

  • 1
    Thanks. I ended up reforming the command to avoid the $pu_ variable but this is a brilliant work around and I think deserves the answer acceptance. – malan Aug 23 '18 at 14:21
-1

If your only problem is to understand how to have a single quote character inside a single quoted string, here is the method. Use:

'\''

for the single quote character inside the single quoted string.

This ends the current quoted string, then inserts a quoted quote and then starts a new quoted string.

BTW: since this is a frequent problem, bosh comes with a method to automate this conversion. You could fetch schilytools and compile them and then play with bosh. If you call:

set -o hashcmds
#b aliasname aliasreplacement

The value in aliasreplacement is entered in raw mode and if you later call:

alias aliasname

you see the alias in the correctly quoted form and if you enter

alias -R aliasname

you see the alias in raw mode.

  • You're saying there's no BASH solution? I thought of escaping the ' but didn't try it until you mentioned it. And yeah, it doesn't work. – malan Aug 23 '18 at 13:58
  • If this escaping really does not work in bash, then you should make a bug report. I however guess that you did not follow my example. – schily Aug 23 '18 at 23:15
  • I did not downvote you. Escaping all the single quotes did not solve the problem, it merely changed it. I cannot recall the exact error message. weirdan's solution worked around the BASH quoting and variable evaluation problems with the command in particular in a simple manner and so I accepted it. I am not going to install software packages with limited support in my operating system (bosh is in the aur only and I try to avoid the aur; schilytools isn't even available except from your sourceforge) in order to solve a problem when there's a simpler solution. – malan Aug 24 '18 at 12:45
  • You do not need to follow my advise, but I believe schilytools are important on a Linux installation, sice they give you e.g. a SHA3 aware hash program, a correctly working tar and a POSIX certified make program. If your distro does not include these programs, you may like to ask your distro why this is missing. – schily Aug 24 '18 at 15:06

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.