5

For an example i would like to login to mysql with a password. i KNOW i can use -pmypass but i want to learn how do i redirect stdin in bash. So my test is

mysql -u limited_user -p <text to redirect into stdin when it prompts for my pass>

I seen < filename before but i dont want to store the data in a file. I tried & and &- but had no luck. I am not sure what &- does.

  • 2
    Be aware that by putting it a pipeline or doing anything except interactive stdin, you allow every user on the system to view the password by looking at the running processes – Daenyth Jul 14 '10 at 15:13
  • Daenyth: I heard that but havent asked about it. Can nonroot users see root user running processes? Is that really an issue? because if no one can see your process unless they are higher then they can access your files and see your password anyways? –  Jul 14 '10 at 15:16
  • 1
    yes, on linux at least, anyone can see the entire process list. – bstpierre Jul 14 '10 at 16:58
  • For the meaning of `&-`, see also now http://stackoverflow.com/questions/38782395/redirecting-the-output-of-shell-command-to-what-does-it-mean – tripleee Aug 05 '16 at 06:33

2 Answers2

12
command <<< "input"

That's a bashism and echo solution is more common in scripts, though my solution can be more handy in an interactive shell (and I doubt any of these solutions works with mysql, but your question is more general).

wRAR
  • 25,009
  • 4
  • 84
  • 97
3

Normally, it's just:

echo password|command

But many programs, including MySQL, that read passwords don't actually read from stdin. Rather, they interact with the terminal directly. See Trick an application into thinking its stdin is interactive, not a pipe.

Community
  • 1
  • 1
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539