0

This is somehow related to Use substituted string as a command in shell script I asked last year. That accepted answer worked nicely.

#!/usr/bin/env bash

for user in ytu
do
  cd /home/${user}/H2-Data/crons
  for path in "$user"_*_monthly_report.py
  do
    if [ -e $path ]
    then
      . ../../.profile
      userpython=${user^^}_PYTHON
      echo ${!userpython} $path
    else
      break
    fi
  done
done

This echos what I expected:

/home/ytu/anaconda3/bin/python ytu_clinic217_monthly_report.py
/home/ytu/anaconda3/bin/python ytu_clinic226_monthly_report.py

However, by simply changing ${user^^}_PYTHON to $YTU_PYTHON, which should be exactly the same in this case, the bash script now echos:

ytu_clinic217_monthly_report.py
ytu_clinic226_monthly_report.py

I even tried userpython=/home/ytu/anaconda3/bin/python but that ends up the same. That said, if I echo $userpython, I can still get /home/ytu/anaconda3/bin/python in the latter cases.

I wonder why can't userpython be evaluated anymore by simply assigning the variable explicitly, and how can I make it right?

ytu
  • 1,822
  • 3
  • 19
  • 42
  • 3
    `${user^^}_PYTHON` expands to `YTU_PYTHON`, which is then the value of `$userpython`. If you use `$YTU_PYTHON`, which is a variable that doesn't exist, `$userpython` contains the empty string. The equivalent to what you have would be `userpython=YTU_PYTHON` (notice lack of `$`). – Benjamin W. Nov 06 '19 at 03:41
  • Run your script with `set -x` to see what gets set where. – Benjamin W. Nov 06 '19 at 03:43

0 Answers0