0

I use Bash

I wrote a command for printing only docker id separate with whitespaces on one line.

docker ps -a | grep -E '^[a-z0-9]{12}' -o | awk '{printf "%s ",$0} END {print ""}'

Works well, so I decided to make it an alias.

First, it seems that the whitespace here "%s " was a problem, so I decided to escape it

The new command looks like this:

docker ps -a | grep -E '^[a-z0-9]{12}' -o | awk '{printf "%s\ ",$0} END {print""}'

Now, no errors when sourcing the .bashrc but when I tried to run my alias I get the following error:

{printf %s ,-bash} END {print}
        ^ syntax error

I already looked at this fish: whitespace in alias but I am not using fish

EDIT, my alias looks like this:

alias dockerid="docker ps -a | grep -E '^[a-z0-9]{12}' -o | awk '{printf "%s\ ",$0} END {print""}'"

Note that if I use single quote, the source command fails

1
  • Note: you alias start with ", but then it contain other " (e.g. in awk command) which in reality are used as end of previous ". You should escape all inner ", e.g. with \" Commented Dec 2, 2022 at 13:31

5 Answers 5

2

This is one of the reasons why the bash manual says:

For almost every purpose, shell functions are preferred over aliases.

dockerid() {
    docker ps -a \
    | grep -o -E '^[a-z0-9]{12}' \
    | awk '{printf "%s ",$0} END {print ""}'
}

Although the alias quoting hell goes away by replacing awk with paste

alias dockerid="docker ps -a | grep -o -E '^[a-z0-9]{12}' | paste -d ' ' -s"
1

Instead of using docker ps -a | grep -E '^[a-z0-9]{12} -o | ...' you can use:

docker ps -a --format '{{.ID}}' | xargs

or with alias (I prefer functions)

alias dockerid='docker ps -a --format "{{.ID}}" | xargs'
2
  • 1
    +1. alternatively, pipe into paste -sd ' ' instead of xargs - which was the answer I was just about to post until I saw yours. Using docker's built-in formatting options is the right solution. No need for grep or awk (and, as always, you never need to pipe grep's output into awk as awk can already do regex matches).
    – cas
    Commented Dec 3, 2022 at 8:48
  • @cas Agree with you! It's worthy to learn about docker's built-in as well as its API. It may help you to do a lot of things without piping several commands (e.g. for getting the output as json). Commented Dec 3, 2022 at 9:13
0

The problem is that you have " inside a "-quoted string. As you can see in your question (look at the syntax highlighting), the first, internal " is taken as the closing ". So you need to escape the internal quotes, and also escape the internal \ , like this:

alias dockerid="docker ps -a | grep -E '^[a-z0-9]{12}' -o | awk '{printf \"%s\\ \",$0} END {print \"\"}'"
2
  • I deleted my previous comment, I was running this on a dockerless server..... Thanks to you and Giacomo I was able to works out this one
    – Admineral
    Commented Dec 2, 2022 at 13:44
  • @Admineral you probably don't want to escape the space anyway. A simple printf "%s ",$0 will work, there is no need for printf "%s\ ",$0 to get the space.
    – terdon
    Commented Dec 2, 2022 at 13:57
0

With the previous comment of @terdon and some other stuff needed to be set on my command I will post the final working alias here:

alias dockerid="docker ps -a | grep -E '^[a-z0-9]{12}' -o | awk '{printf \"%s \",\$0} END {print \"\"}'"

Just like @terdon mentions, no need to escape the space.

Moreover I have to escape $0, if not this would only print 0, as it would be interpreted literraly

0

The problem is that you are using single quotes (') to enclose the entire command that you want to run when the alias is called, but you are also using single quotes within the command itself. This is causing the shell to interpret the single quotes in the command as being part of the alias definition, rather than part of the command that should be run.

To fix this, you can use double quotes (") instead of single quotes to enclose the entire command. This will allow the shell to interpret the single quotes within the command as part of the command itself, rather than part of the alias definition.

alias dockerid="docker ps -a | grep -E '^[a-z0-9]{12}' -o | awk '{printf "%s\ ",$0} END {print\"\"}'"

This should work for your case.

You must log in to answer this question.

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