I am with CentOS 7 and I want to bind an alias to launch PostgreSQL shell(psql). I defined this alias and append it in /etc/profile.d/alias:
alias psql-local="read -p \"PSQL: enter the DB to connect: \" db ; sudo -i -u postgres psql --dbname $db"
It is executable by root.
And, I login as root, and run alias, I get:
alias psql-local='read -p "PSQL: enter the DB to connect: " db ; sudo -i -u postgres psql --dbname '
Note here $db at the end is empty.
Then I run psql-local, but I get error:
[root@lucas_vm ~]
> psql-local
PSQL: enter the DB to connect: jfps
psql: option '--dbname' requires an argument
Try "psql --help" for more information.
Then I enter /etc/profile.d/, and run alias.sh manually, then suddenly I can use this alias:
[root@lucas_vm /etc/profile.d]
> . alias.sh
[root@lucas_vm /etc/profile.d]
> psql-local
PSQL: enter the DB to connect: jfps
psql (10.5)
Type "help" for help.
jfps=#
If I exit psql and run alias again, I see this line changed:
alias psql-local='read -p "PSQL: enter the DB to connect: " db ; sudo -i -u postgres psql --dbname jfps'
Note $db is changed to jfps.
Then, I try to access another database, and it works again.
But, when I exit and alias, I see the --dbname jfps, not the name of second database. When I echo $db, it, instead, is the name of the second db.
Why?