I'm trying to copy the values of a variable to clipboard from my password generator but it doesn't work
up vote
1
down vote
favorite
#!/bin/bash
echo "How many characters?
read length
pass=sudo head /dev/urandom | tr -dc 'A-Za-z0-9!"#$@&%?' | head -c$length
sleep 1
echo "$pass"
echo $pass | xclip -sel c
It works if I change out $pass
with $length
but that's not what I want to copy to the clipboard
bash scripts
add a comment |Â
up vote
1
down vote
favorite
#!/bin/bash
echo "How many characters?
read length
pass=sudo head /dev/urandom | tr -dc 'A-Za-z0-9!"#$@&%?' | head -c$length
sleep 1
echo "$pass"
echo $pass | xclip -sel c
It works if I change out $pass
with $length
but that's not what I want to copy to the clipboard
bash scripts
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
#!/bin/bash
echo "How many characters?
read length
pass=sudo head /dev/urandom | tr -dc 'A-Za-z0-9!"#$@&%?' | head -c$length
sleep 1
echo "$pass"
echo $pass | xclip -sel c
It works if I change out $pass
with $length
but that's not what I want to copy to the clipboard
bash scripts
#!/bin/bash
echo "How many characters?
read length
pass=sudo head /dev/urandom | tr -dc 'A-Za-z0-9!"#$@&%?' | head -c$length
sleep 1
echo "$pass"
echo $pass | xclip -sel c
It works if I change out $pass
with $length
but that's not what I want to copy to the clipboard
bash scripts
edited May 3 at 20:55
heemayl
63.8k8126202
63.8k8126202
asked May 3 at 20:19
Eddie
83
83
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
So many things to look at, but assuming your initial question is about saving the output of a command as a variable.
To save the output (STDOUT) of some command or pipeline as a variable, you need to use command substitution ($()
):
pass=$(head /dev/urandom | tr -dc 'A-Za-z0-9!"#$@&%?' | head -c "$length")
I have made two changes:
you don't need to be
root
to read the/dev/urandom
file, so I've droppedsudo
always quote variable expansions (unless you know what you're doing); I've quoted
$length
Also, read
can show a prompt string (see -p
option), you don't need to use echo
:
read -p 'How many characters?' length
I'm still a beginner at this stuff so that echo stuff is what we have learned at school. Aside from that it seems the problem was fixed with the command substitution.
â Eddie
May 3 at 20:43
So if command substitution isn't there it doesn't save the output to the variable but echo "$pass" worked in showing the value, why is that?
â Eddie
May 3 at 20:50
@Eddie What didecho $pass
show? Without command substitution, the code syntax you've used originally would show you error(s).
â heemayl
May 3 at 20:56
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
So many things to look at, but assuming your initial question is about saving the output of a command as a variable.
To save the output (STDOUT) of some command or pipeline as a variable, you need to use command substitution ($()
):
pass=$(head /dev/urandom | tr -dc 'A-Za-z0-9!"#$@&%?' | head -c "$length")
I have made two changes:
you don't need to be
root
to read the/dev/urandom
file, so I've droppedsudo
always quote variable expansions (unless you know what you're doing); I've quoted
$length
Also, read
can show a prompt string (see -p
option), you don't need to use echo
:
read -p 'How many characters?' length
I'm still a beginner at this stuff so that echo stuff is what we have learned at school. Aside from that it seems the problem was fixed with the command substitution.
â Eddie
May 3 at 20:43
So if command substitution isn't there it doesn't save the output to the variable but echo "$pass" worked in showing the value, why is that?
â Eddie
May 3 at 20:50
@Eddie What didecho $pass
show? Without command substitution, the code syntax you've used originally would show you error(s).
â heemayl
May 3 at 20:56
add a comment |Â
up vote
2
down vote
accepted
So many things to look at, but assuming your initial question is about saving the output of a command as a variable.
To save the output (STDOUT) of some command or pipeline as a variable, you need to use command substitution ($()
):
pass=$(head /dev/urandom | tr -dc 'A-Za-z0-9!"#$@&%?' | head -c "$length")
I have made two changes:
you don't need to be
root
to read the/dev/urandom
file, so I've droppedsudo
always quote variable expansions (unless you know what you're doing); I've quoted
$length
Also, read
can show a prompt string (see -p
option), you don't need to use echo
:
read -p 'How many characters?' length
I'm still a beginner at this stuff so that echo stuff is what we have learned at school. Aside from that it seems the problem was fixed with the command substitution.
â Eddie
May 3 at 20:43
So if command substitution isn't there it doesn't save the output to the variable but echo "$pass" worked in showing the value, why is that?
â Eddie
May 3 at 20:50
@Eddie What didecho $pass
show? Without command substitution, the code syntax you've used originally would show you error(s).
â heemayl
May 3 at 20:56
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
So many things to look at, but assuming your initial question is about saving the output of a command as a variable.
To save the output (STDOUT) of some command or pipeline as a variable, you need to use command substitution ($()
):
pass=$(head /dev/urandom | tr -dc 'A-Za-z0-9!"#$@&%?' | head -c "$length")
I have made two changes:
you don't need to be
root
to read the/dev/urandom
file, so I've droppedsudo
always quote variable expansions (unless you know what you're doing); I've quoted
$length
Also, read
can show a prompt string (see -p
option), you don't need to use echo
:
read -p 'How many characters?' length
So many things to look at, but assuming your initial question is about saving the output of a command as a variable.
To save the output (STDOUT) of some command or pipeline as a variable, you need to use command substitution ($()
):
pass=$(head /dev/urandom | tr -dc 'A-Za-z0-9!"#$@&%?' | head -c "$length")
I have made two changes:
you don't need to be
root
to read the/dev/urandom
file, so I've droppedsudo
always quote variable expansions (unless you know what you're doing); I've quoted
$length
Also, read
can show a prompt string (see -p
option), you don't need to use echo
:
read -p 'How many characters?' length
answered May 3 at 20:28
heemayl
63.8k8126202
63.8k8126202
I'm still a beginner at this stuff so that echo stuff is what we have learned at school. Aside from that it seems the problem was fixed with the command substitution.
â Eddie
May 3 at 20:43
So if command substitution isn't there it doesn't save the output to the variable but echo "$pass" worked in showing the value, why is that?
â Eddie
May 3 at 20:50
@Eddie What didecho $pass
show? Without command substitution, the code syntax you've used originally would show you error(s).
â heemayl
May 3 at 20:56
add a comment |Â
I'm still a beginner at this stuff so that echo stuff is what we have learned at school. Aside from that it seems the problem was fixed with the command substitution.
â Eddie
May 3 at 20:43
So if command substitution isn't there it doesn't save the output to the variable but echo "$pass" worked in showing the value, why is that?
â Eddie
May 3 at 20:50
@Eddie What didecho $pass
show? Without command substitution, the code syntax you've used originally would show you error(s).
â heemayl
May 3 at 20:56
I'm still a beginner at this stuff so that echo stuff is what we have learned at school. Aside from that it seems the problem was fixed with the command substitution.
â Eddie
May 3 at 20:43
I'm still a beginner at this stuff so that echo stuff is what we have learned at school. Aside from that it seems the problem was fixed with the command substitution.
â Eddie
May 3 at 20:43
So if command substitution isn't there it doesn't save the output to the variable but echo "$pass" worked in showing the value, why is that?
â Eddie
May 3 at 20:50
So if command substitution isn't there it doesn't save the output to the variable but echo "$pass" worked in showing the value, why is that?
â Eddie
May 3 at 20:50
@Eddie What did
echo $pass
show? Without command substitution, the code syntax you've used originally would show you error(s).â heemayl
May 3 at 20:56
@Eddie What did
echo $pass
show? Without command substitution, the code syntax you've used originally would show you error(s).â heemayl
May 3 at 20:56
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1031719%2fim-trying-to-copy-the-values-of-a-variable-to-clipboard-from-my-password-genera%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password