bashshellterminalzenity

How do I make ```zenity --password``` input into a variable?


How do I make zenity --password input into a variable?

I'm using zenity --password to get input from the user and I'm trying to save it as a variable.

Here are some commands I've tried:

  1. var=${zenity --password};
  2. zenity var=--password
  3. var=zenity --password

None of these work.

Error Codes:

I got error codes from commands #2 and #3. The command #2 error code was:

You must specify a dialog type. See 'zenity --help' for details

The command #3 error code was:

-bash: --password: command not found.

I'm not that familiar with bash, and I only know a few basic commands. The --password command is from Zenity (sudo apt-get install zenity). If somebody answers, thank you for helping!

Links

Zenity Manual - https://help.gnome.org/users/zenity/stable/

Password Command - https://help.gnome.org/users/zenity/stable/password.html.en


Solution

  • The -S (stdin) option causes sudo to read the password from the standard input instead of the terminal device. The password must be followed by a newline character.

    zenity --password | sudo -S some_app_here
    

    Example

    #!/bin/bash
    
    PASSWORD=$(zenity --password)
    echo $PASSWORD | sudo -S "command"
    
    echo $PASSWORD | sudo -S "command"
    
    exit