0

I'm trying to run a simple shell ( Centos 7 ) that print out the system HOSTNAME variable:

test.sh

#!/bin/ksh
echo "HOSTNAME:"$HOSTNAME

when executed from bash or ksh the shell does not work as expected

>ksh ./test.sh ----> HOSTNAME is unset
>./test.sh ----> HOSTNAME is unset

if i create a ~/.profile that declare and export HOSTNAME nothing change. if i crate a ~./kshrc that source ~./profile works only if i switch from bash to ksh and execute the script.

if i run from bash the following code it does work due to the interactive mode

ksh -xi ./test.sh

+ command . ./.profile
+ HOSTNAME=<MYNAME>
+ export HOSTNAME
+ export HOSTNAME
+ echo HOSTNAME:<MYNAME>
HOSTNAME:<MYNAME>

without interactive mode ~/.profile is not loaded

+ echo HOSTNAME:
HOSTNAME:

I need to have original HOSTNAME set even in ksh due to a bunch of scripts that use HOSTNAME variable

PS: i've tried printing PATH variable and it's working even when HOSTNAME does not

Could you please give me some advice?

over
  • 1
  • 2
  • You need to `export HOSTNAME`. This is almost certainly a duplicate. – tripleee Dec 12 '20 at 09:10
  • Your `.kshrc` should almost certainly not source `.bashrc`. If you want to share settings, put them in `.profile` - and make sure you don't use any Bash or Ksh syntax - and then `source` that from your shell-specific startup files if necessary. (For example, Bash prefers `.bash_profile` over `.profile` if both exist; the former should usually `source` the latter.) – tripleee Dec 12 '20 at 09:13
  • If the variable is already `export`ed, something is replacing its value. If this is a private variable of yours, don't use uppercase because that conflicts with a system variable. See also https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization – tripleee Dec 12 '20 at 09:16
  • Without access to your startup files, it's haid to tell. Read the `ksh` manual page and/or experiment with its debugging options (guessing `ksh -xi`; not in a place where I can test). – tripleee Dec 12 '20 at 09:25
  • `.profile` vs `.bashrc` is probably unrelated to your problem, but overloading `.bashrc` for stuff which isn't specific to Bash is generally problematic. The `.profile` file is available for precisely this purpose. – tripleee Dec 12 '20 at 09:27
  • Probably [edit] your question to include these debugging efforts, and delete these comments - your question should be self-contained. It's still not clear if you want to override the system variable of the same name, or just unluckily gave your private variable a name which clashes with the reserved system variable (in which case the trivial fix is to rename it to lower case, and generally avoid uppercase for your private variables). – tripleee Dec 12 '20 at 09:34
  • @tripleee thanks edited original message . hope it's more clear – over Dec 12 '20 at 09:52
  • are any other system variables working like PATH? echo them and see if that works, then I would detect where those are defined .... maybe /etc/profile or your home dir profile.... –  Dec 12 '20 at 09:55
  • The root problem here is probably that `HOSTNAME` is reserved by Bash. It will be automatically set when you are running Bash; perhaps your scripts which require this variable should then be changed to require Bash (`#!/bin/bash` instead of `#!/bin/sh` in the shebang, etc), or use a different variable name if you don't want that. – tripleee Dec 12 '20 at 10:07

1 Answers1

0

When I put

HOSTNAME=$(cat /etc/hostname)
export HOSTNAME

in my .kshrc - if you don't have it, create it, I got this working and was able to

echo $HOSTNAME
server