简体   繁体   中英

How to read hidden input from terminal and pipe it to another command

I would like to be able securely type text in terminal and pipe it to another command to:

  • Not be recorded in terminal history
  • Be hidden as you type it
  • Not be recorded in any file or environmental variable
  • Be in memory for shortest possible time

Ideally:

  • Using commonly installed tools on linux
  • Easy to use as echo
  • Not having to create any scripts/files
  • Can be piped to other commands

Example of non secure input

echo "secret" | wc -c

Almost what I want:

read -s | wc -c

Basically the same way how you input password to sudo and similar.

My use case

echo "secret" | gpg --encrypt --armor -r 1234567890ABCDEF | xclip

I am looking for a way with restrictions I mentioned in points above. Knowing that what I am looking for doesn't exist is also an answer I will accept and mark.

I created alias from accepted answer

alias secnote="{ read -s; printf %s $REPLY; } | gpg --encrypt --armor -r 123467890ABCDEF | pbcopy"

Is this what you wanted to achieve?

$ read -s       # I type `secret`
$ echo $REPLY
secret
$ printf %s $REPLY | wc -c
6
$ unset REPLY
$ echo $REPLY
# empty now

Or you want one-liner like this:

{ read -s -p "Input a secret: "; printf %s $REPLY; } | wc -c

If you define an alias:

alias readp='{ read -s -p "Input a secret: "; printf %s $REPLY; }'

then you can do readp | wc -c readp | wc -c

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM