Ruslan Plastun
Ruslan Plastun

Reputation: 2252

Can't understand simple bash script

I am very new to bash scripts so I can't really understand what this line below does:

[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config

This double square brackets is an if statement I guess. And I understand that we transform "Host *\n\tStrictHostKeyChecking no\n\n" into config file, but what is everything else over there. Some documentation or tutorials for newbie is very appreciated. Thanks in advance!

Upvotes: 1

Views: 798

Answers (2)

Alan Birtles
Alan Birtles

Reputation: 36614

[[ ... ]] is a function that evaluates the statement inside it and returns either true or false.

-f means "file exists": http://tldp.org/LDP/abs/html/fto.html

&& means "and" which means execute the next statement if the previous statement returned true

echo -e means print the string in the second argument to stdout and interpret escape sequences e.g. "\n".

> ~/.ssh/config means write the stdout of the preceding statement to "~/.ssh/config"

The long hand and possibly easier to understand version of this would be:

if [[ -f /.dockerenv ]]; then
   echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
fi

Upvotes: 7

Ondrej K.
Ondrej K.

Reputation: 9679

In short: It tells ssh to automatically add host keys to known hosts regardless of whether it's knows or changed.

It's a shorthand for:

if [[ -f /.dockerenv ]]; then
    echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
fi

[[...]] is bash built-in test. And && means, the following command is run if the previous has succeeded (returned 0). I.e. if /.dockerenv file exists and is a file, it echos (interpreting backslash escapes -e):

Host *
        StrictHostKeyChecking no

Followed by two training newlines into ~/.ssh/config (replacing any content that might have been there). It however assumes the directory ~/.ssh to already exist and would fail if it did not.

Upvotes: 4

Related Questions