Configuring terminal and referencing username in bash scripts.

Linux for blind general discussion blinux-list at redhat.com
Thu Apr 27 09:05:45 UTC 2023


On 4/26/23 21:22, Linux for blind general discussion wrote:
> Okay, so I've been using a ~/.bash_profile file with the following contents:
>
> rm -f ~/.bash_history

$ history -c

> export PS1='$(tty | sed 's#^/dev/tty##')\$'
> export PATH=~/Programming/bash-scripts:$PATH

${PATH} is better when you create a string

> Also, I have some scripts to automate sshing into some remote hosts or
> mounting the remote filesystems locally, and part of it involves
> creating a mounttt point that needs to be chown to my user. Is there a
> shell variable I can use to make these scripts work for any user
> instead of needing to edit the script to use the name of the user I'm
> logged in as?

$ USER_NAME=$USER sh try.sh
$ cat try.sh
echo $USER_NAME

$ sh try.sh $USER
$ cat try.sh
echo $1

$ sh try.sh
echo $USER

$ try.sh -u $USER
$ cat try.sh
#!/bin/sh

while getopts ':u:' options; do
     case $options in
         u)
             user=$OPTARG
             ;;
         *)
             printf "Error: option %s not recognized at index %s\n"
$OPTARG $((OPTIND-1)) >&2
             exit 1
             ;;
     esac
done

shift $((OPTIND - 1))

echo $user

HTH.

--
John Doe



More information about the Blinux-list mailing list