Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

87%
+12 −0
Q&A How to give password in shell script?

That's my bash script. Actually, no, it isn't. Assuming for a second that the shebang line is #!/bin/sh (not #!bin/sh as you have it in the question), it's a sh shell script, not a bash shell ...

posted 3y ago by Canina‭  ·  edited 2y ago by Canina‭

Answer
#2: Post edited by user avatar Canina‭ · 2021-08-13T21:59:28Z (over 2 years ago)
  • > That's my bash script.
  • Actually, no, it isn't. Assuming for a second that the shebang line is `#!/bin/sh` (not `#!bin/sh` as you have it in the question), it's a sh shell script, not a bash shell script. Assuming that your /bin/sh is actually bash (which it need not be; for example, Debian uses `dash` as `sh`), bash will start in a different mode when invoked as `sh`. Other shells may do things differently when invoked as `sh` or as some other name, or they might not.
  • The distinction is unlikely to make any difference with such a simple shell script as you have in the question, but for more advanced features, it can make a very real difference.
  • **That said**, ignoring the fact that this is through a shell script, it seems that your question actually is **"how do I invoke the `mysql` client such that it does not prompt interactively for a MySQL user's password, but authenticates using a password?"**.
  • The answer to that is in the `mysql(1)` man page.
  • A bare `mysql -p` asks the MySQL client to enter password authentication mode.
  • **The `-p` can also take a password,** either as `--password=qwerty` or `-pqwerty` (note the absence of a space separating `-p` and the password).
  • The man page also notes that passing a password on the command line is insecure ([this answer of mine](https://software.codidact.com/posts/277511#answer-277517 "Is it unsecure to use a password on the command line to run a MySQL script on Windows?") has a more in-depth discussion on why that is).
  • Note that when invoked in this manner, the shell executing the script will wait for the `mysql` process to exit before continuing with the `rails` command. That's probably not what you actually want. To execute a command in the background and continue with the execution of the script without waiting for that command to actually finish, you would use `&` to background the process, but since `mysql` is an interactive tool, that would make little sense here.
  • > That's my bash script.
  • Actually, no, it isn't. Assuming for a second that the shebang line is `#!/bin/sh` (not `#!bin/sh` as you have it in the question), it's a sh shell script, not a bash shell script. Assuming that your /bin/sh is actually bash (which it need not be; for example, Debian uses `dash` as `sh`), bash will start in a different mode when invoked as `sh`. Other shells may do things differently when invoked as `sh` or as some other name, or they might not.
  • The distinction is unlikely to make any difference with such a simple shell script as you have in the question, but for more advanced features, it can make a very real difference.
  • **That said**, ignoring the fact that this is through a shell script, it seems that your question actually is **"how do I invoke the `mysql` client such that it does not prompt interactively for a MySQL user's password, but authenticates using a password?"**.
  • The answer to that is in the `mysql(1)` man page.
  • A bare `mysql -p` asks the MySQL client to enter password authentication mode.
  • **The `-p` can also take a password,** either as `--password=qwerty` or `-pqwerty` (note the absence of a space separating `-p` and the password).
  • The man page also notes that passing a password on the command line is insecure ([this answer of mine](https://software.codidact.com/posts/277511/277517#answer-277517 "Is it unsecure to use a password on the command line to run a MySQL script on Windows?") has a more in-depth discussion on why that is).
  • Note that when invoked in this manner, the shell executing the script will wait for the `mysql` process to exit before continuing with the `rails` command. That's probably not what you actually want. To execute a command in the background and continue with the execution of the script without waiting for that command to actually finish, you would use `&` to background the process, but since `mysql` is an interactive tool, that would make little sense here.
#1: Initial revision by user avatar Canina‭ · 2021-07-18T15:36:41Z (almost 3 years ago)
> That's my bash script.

Actually, no, it isn't. Assuming for a second that the shebang line is `#!/bin/sh` (not `#!bin/sh` as you have it in the question), it's a sh shell script, not a bash shell script. Assuming that your /bin/sh is actually bash (which it need not be; for example, Debian uses `dash` as `sh`), bash will start in a different mode when invoked as `sh`. Other shells may do things differently when invoked as `sh` or as some other name, or they might not.

The distinction is unlikely to make any difference with such a simple shell script as you have in the question, but for more advanced features, it can make a very real difference.

**That said**, ignoring the fact that this is through a shell script, it seems that your question actually is **"how do I invoke the `mysql` client such that it does not prompt interactively for a MySQL user's password, but authenticates using a password?"**.

The answer to that is in the `mysql(1)` man page.

A bare `mysql -p` asks the MySQL client to enter password authentication mode.

**The `-p` can also take a password,** either as `--password=qwerty` or `-pqwerty` (note the absence of a space separating `-p` and the password).

The man page also notes that passing a password on the command line is insecure ([this answer of mine](https://software.codidact.com/posts/277511#answer-277517 "Is it unsecure to use a password on the command line to run a MySQL script on Windows?") has a more in-depth discussion on why that is).

Note that when invoked in this manner, the shell executing the script will wait for the `mysql` process to exit before continuing with the `rails` command. That's probably not what you actually want. To execute a command in the background and continue with the execution of the script without waiting for that command to actually finish, you would use `&` to background the process, but since `mysql` is an interactive tool, that would make little sense here.