have to determine all users home directories - tilde scripting problem

后端 未结 6 1533
臣服心动
臣服心动 2021-01-03 04:04

Assume someuser has a home directory /home/someuser

NAME=someuser

In bash - what expression to I use combining tilde (~) and $NAME to return the users home d

6条回答
  •  忘掉有多难
    2021-01-03 04:53

    Safer:

    eval HOMEDIRECTORY="$(printf "~%q" "$NAME")"
    

    Here the %q option to printf quotes and escapes dangerous characters.

    If $NAME is joe, you'd get something like /home/joe. For root, you might get /root. For "abc;rm something" you'd get "~abc;rm something" instead of having something removed.

提交回复
热议问题