I'm trying to find a way to shorten these lines (CI_PRIVATE_SSH_KEY is already set):

mkdir -p "${HOME}/.ssh/"
chmod 700 "${HOME}/.ssh/"
echo "${CI_PRIVATE_SSH_KEY}" > "${HOME}/.ssh/id_rsa"
chmod 600 "${HOME}/.ssh/id_rsa"

This is what I have so far:

(umask 077; mkdir -p "${HOME}/.ssh/"; echo "${CI_PRIVATE_SSH_KEY}" > "${HOME}/.ssh/id_rsa")

But this doens't quite work, because the directory is getting created with no group + other permissions.

So my question is: is there a umask value that let me create directories with 700 and files with 600 (provided the default value is 0022)?

I've also tried 177, but it doesn't work either.

  • 1
    Out of curiosity, why do you need to shorten the commands? Specifying the permissions makes it clear and obvious what your intentions are, versus relying on a umask.
    – Jeff Schaller
    Commented Sep 17, 2019 at 22:17
  • Really not a big deal; I'm just trying to avoid longer debug streams because the CI process spits out some bits for every line that gets executed — I can't control that, it's set at the org level.
    – user280674
    Commented Sep 17, 2019 at 22:29

2 Answers 2


umask 077 will:

  • create directories with permission 700
  • create files with permission 600
    ➜ umask 077
    ➜ mkdir foo; ls -ld foo
    drwx------  2 richard  staff  64 13 Oct 12:42 foo
    ➜ touch foo/bar; ls -l foo/bar
    -rw-------  1 richard  staff  0 13 Oct 12:42 foo/bar

i agreed with @Jeff Schaller, chmod is clear and obvious versus umask.
try to build one liner with && operator
and the preferred permission of .ssh directory changed to 644
bash would try to run the command from left to right of the && operator
and only continues when all the "left side" command return status is 0

mkdir -p "${HOME}/.ssh/" && chmod 644 "${HOME}/.ssh/" && echo "${CI_PRIVATE_SSH_KEY}" > "${HOME}/.ssh/id_rsa" && chmod 600 "${HOME}/.ssh/id_rsa"

or umask 133 for .ssh , 177 for the key,

(umask 133; mkdir -p "${HOME}/.ssh/";umask 177; echo "${CI_PRIVATE_SSH_KEY}" > "${HOME}/.ssh/id_rsa")
  • OK, fair enough...good point. I can go with this one...thanks!
    – user280674
    Commented Sep 19, 2019 at 11:15

You must log in to answer this question.