0

I'm looking for a way (preferably cross-platform compatible) to set something globally accessible from a bash script.

My company is using a bash script to request access credentials to a mysql database. This returns username, password and db domain that I end up having to copy paste in my terminal to run and connect to our mysql db.

I thought i'd amend the script to set environment variables and make use of these in an alias with the credentials set in my bashrc but turns out you can't set environment variables in a bash script.

So i tried to set the mysql alias with the username password and domain pre-filled in that same script but same issue. Can't set an alias in a bash script.

I essentially want to be able to run the script that gives me the credentials and then not have to do manual copy pasting all over the place.

What I tried was (if it give more context):

#!/bin/bash
# Script gets the credentials
# Script now has username, password, endpoint variables
export MYSQL_USER=$username
export MYSQL_PASSWORD=$password
export MYSQL_ENDPOINT=$endpoint
# Script finishes

and in my bashrc:

alias mysqlenv="mysql -h $MYSQL_ENDPOINT -u $MYSQL_USER -p'$MYSQL_PASSWORD'"

I appreciate this is not working and that might not be the best solution so i'm open to other options.

PS: Forgot to mention the credentials expire every 24H which is why i want to smoothen the process

  • What do you mean, "globally"? Normal shell variables, ones you haven't even `export`ed, are "global" in the normal sense of the term, that they're shared by all code running inside the same language interpreter; one has to go out of their way (using `local`, `declare` or `typeset` in a function) to make a shell variable local. Of course, in *every* programming language -- bash included -- when a program exits, its variables are no longer in memory. – Charles Duffy Apr 22 '20 at 19:31
  • 1
    "you can't set environment variables in a bash script" Sure you can, but you need to do `source script.sh` instead of `bash script.sh`. – ceejayoz Apr 22 '20 at 19:32
  • BTW, note that in general, aliases are a bad idea. It's harder to get a function call wrong. And setting a password on a command line is a **really** bad idea, since users can read each others' command lines. – Charles Duffy Apr 22 '20 at 19:34
  • `mysql` can get the username and password to use straight from the environment, and it's much safer to let it do that instead of copying them to the command line as well. On modern Linux, environment variables can only be read by the current user and by root -- which is to say, by the same accounts that can read your current user's files as well, so an environment variable is effectively just as safe as an unencrypted dotfile. – Charles Duffy Apr 22 '20 at 19:34
  • @ceejayoz `source` won't run the script properly though @CharlesDuffy I meant globally system wise like an environment variable. For the setting passwords from the command line, yes although not an issue here as the password if to a development database and expires after 24H. As for getting the password from environment, that's great but is there a way for me to run the script that get my credentials and set those variables? – William Iehl Apr 22 '20 at 19:42
  • I'm sure there is a standard way to achieve what I want, I'm just not aware of it – William Iehl Apr 22 '20 at 19:47
  • @CharlesDuffy This is not a duplicate, sourcing my script doesn't work, it's not just exporting environment variables, it's getting arguments from the cli and using those to open a tab in my browser to log me in out company system and fetch credentials from vault – William Iehl Apr 22 '20 at 19:58
  • If you use `source`, then you *can* "set an alias in a bash script" and you *can* also "set environment variables in a bash script". Those are the places where the question as currently written describes you as being blocked. If you have a narrow, specific technical problem that isn't solved by using `source`, then by all means, [edit] to provide a reproducer for that [mre] someone else can test to see the issue themselves, and to use to concretely determine when their answer is adequate to resolve your problem. – Charles Duffy Apr 22 '20 at 22:43
  • BTW, if you want to put something in your `.bashrc` that will modify environment variables when run, that's a good use case for a shell function. `somefunc() { somevar=$(...); }` will set `somevar` to the output of a new copy of whatever command goes in `...` when you run the `somefunc` command, f/e. – Charles Duffy Apr 22 '20 at 22:45

0 Answers0