1

I used to have aliases like:

alias mount-open="sudo mount $(sudo blkid | gawk '/2tb-open/ { print substr($1, 0, 9) }') 2tb-open"

I can't be 100% sure that it was the same, but 100% it was a bash alias, not a bash function, and it had other command inclusion without the use of variables etc.

Now I can't make this work. If I put the double quotes (like in the example), it tries to run some sudo command on terminal startup. If I use single quotes and escape the quotes in the middle:

alias mount-open='sudo mount $(sudo blkid | gawk \'/2tb-open/ { print substr($1, 0, 9) }\') 2tb-open'

it says:

bash: .bashrc: line 25: syntax error near unexpected token `('

I suppose it has something to do with 'extra code' in the default Ubuntu bashrc compared to the Fedora one, which is rather lean. I mean, it worked on Ubuntu. A couple of years ago I installed Fedora and copied the bashrc file from my Ubuntu sytem to it, the aliases etc. worked. It worked in Fedora 32 (with the Ubuntu bashrc) a week ago. But now I had sort of a data loss and I'm trying to 'reinvent' this. Currently I'm using the default Fedora bashrc file, which had only like 15 lines initially

Could anyone suggest how to fix this?

Thank you.

3

2 Answers 2

3

It would be much easier with a function or a script:

mount-open() {
  sudo mount "$(
      sudo blkid |
        gawk '/2tb-open/ { print substr($1, 0, 9) }'
    )" 2tb-open
}

Though if you want to mount the device with label 2tb-open,

mount-open() { sudo mount LABEL=2tb-open 2tb-open; }

should be enough.

2

Single quotes can't be escaped inside a single quoted string. You need to quit the double quotes and reenter them later:

alias mount-open='sudo mount $(sudo blkid | gawk '\''/2tb-open/ { print substr($1, 0, 9) }'\'') 2tb-open'
5
  • May I ask: did you mean 'single quotes cannot be escaped in a single quote string, while they can be escaped in the "unquoted bash command"? I.e. in gawk '\'' the first quote closes the opening one (not shown). The second \' is an escaped quote passed directly to gawk, and the third one starts a single quoted string.
    – d.k
    Commented Oct 2, 2020 at 10:39
  • 1
    @user907860 Yes, this is what was meant.
    – AdminBee
    Commented Oct 2, 2020 at 10:52
  • It would arguably be easier to use a shell function in this case.
    – Kusalananda
    Commented Oct 2, 2020 at 11:45
  • 1
    @Kusalananda: Definitely, but not a bash function was stated in the question.
    – choroba
    Commented Oct 2, 2020 at 11:48
  • 1
    Ah, I thought that was the user trying to recall what the had before, not what they wanted now. Seeing as functions are much more flexible than aliases, and in this case provides a safer quoting environment, a function may possible be a good alternative at least.
    – Kusalananda
    Commented Oct 2, 2020 at 11:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .