Why does sourcing a script with “set var = value”

2020-05-09 01:10发布

I am trying to configure auto logout on my Centos VM. I have noticed that if I create a file at /etc/profile.d/autologout.sh with only set autologout = 30 in the file, then it breaks passing arguments for any script that sources /etc/profile.

A sample script that shows this is:

#!/bin/bash 
source /etc/profile 
echo ${@}

When I run it, the script only sees the arguments "autologout 30" and it doesn't get any parameter I try to pass it when running it.

This occurs regardless of the name of the autologout script, the name of the property, or if I have set autologout 30 instead.

Can someone please explain what is happening? It is as if autologout.sh is hijacking the arguments. I am at a loss for what is happening, and researching profile.d and the set command has turned up nothing.

1条回答
太酷不给撩
2楼-- · 2020-05-09 01:31

set is not used to modify shell variables in POSIX-compliant shells. Rather, when given positional arguments, it modifies the command-line argument list.

If you just want to assign a value to a variable, don't use set. Instead, your file should just contain:

autologout=30

...and "$@" will remain in its original state.

查看更多
登录 后发表回答