What does the set command do

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the bash category.

Last Updated: 2024-04-18

I encountered the following while working on a Dockerfile entrypoint

set -- php-fpm "$@"

What's going on?

The set command - when not used to set options, sets the positional parameters in bash.

# notice how `set` takes multiple arguments
$ set foo bar dog
$ echo $1
foo
$ echo $2
bar
$ echo $3
dog

$@ contains all the existing position parameters. Therefore set -- php-fpm "$0" places php-fpm before $1, $2.

See for yourself:

$ set foo bar dog
$ set -- php-fpm "$@"
$ echo $1,$2,$3,$4   
php-fpm,foo,bar,dog 

Resources