I'm trying to copy the values of a variable to clipboard from my password generator but it doesn't work

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








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







share|improve this question


























    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







    share|improve this question
























      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







      share|improve this question














      #!/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









      share|improve this question













      share|improve this question




      share|improve this question








      edited May 3 at 20:55









      heemayl

      63.8k8126202




      63.8k8126202










      asked May 3 at 20:19









      Eddie

      83




      83




















          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 dropped sudo


          • 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





          share|improve this answer




















          • 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 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










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "89"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          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






























          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 dropped sudo


          • 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





          share|improve this answer




















          • 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 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














          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 dropped sudo


          • 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





          share|improve this answer




















          • 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 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












          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 dropped sudo


          • 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





          share|improve this answer












          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 dropped sudo


          • 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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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 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
















          • 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 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















          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












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          pylint3 and pip3 broken

          Missing snmpget and snmpwalk

          How to enroll fingerprints to Ubuntu 17.10 with VFS491