When I run command bellow in bash shell :
if [[ $(readonly | cut -d= -f1 | grep -qo HISTFILE && echo $?) == 0 ]]; then sudo grep -iwo "readonly HISTFILE" /etc/profile /etc/profile.d/*; else echo "HISTFILE not set or not set properly"; fi The command above return the result with what I want, that is :
/etc/profile.d/os-security.sh:readonly HISTFILE But when I put the command above in shell script, the result change, that is :
HISTFILE not set or not set properly Here the full shell :
[ansible@rhel8-hardening-test ~]$ if [[ $(readonly | cut -d= -f1 | grep -qo HISTFILE && echo $?) == 0 ]]; then sudo grep -iwo "readonly HISTFILE" /etc/profile /etc/profile.d/*; else echo "HISTFILE not set or not set properly"; fi /etc/profile.d/os-security.sh:readonly HISTFILE [ansible@rhel8-hardening-test ~]$ [ansible@rhel8-hardening-test ~]$ [ansible@rhel8-hardening-test ~]$ [ansible@rhel8-hardening-test ~]$ cat test.sh #!/bin/bash if [[ $(readonly | cut -d= -f1 | grep -qo HISTFILE && echo $?) == 0 ]]; then sudo grep -iwo "readonly HISTFILE" /etc/profile /etc/profile.d/*; else echo "HISTFILE not set or not set properly"; fi [ansible@rhel8-hardening-test ~]$ [ansible@rhel8-hardening-test ~]$ [ansible@rhel8-hardening-test ~]$ [ansible@rhel8-hardening-test ~]$ bash test.sh HISTFILE not set or not set properly [ansible@rhel8-hardening-test ~]$ How can I get the same result like current shell do while using shell script with command above ??
#!/bin/bashin a script needs and the HIST* variable may simply not be set. But check the bash manual invocation section and which files get loaded when by bash.sudolike you’re doing from profile.d assumes that every single one of your users is allowed unrestricted NOPASS root rights. When they don’t they’ll be prompted by sudo to enter their password and possibly get an error every time they log on.