Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am writing a bash script and would like to ssh into a remote machine and execute the adduser command. This is not working, all that I get is the --help text for adduser when I run the code below.

ssh [email protected] 'useradd $username; mkdir /home/$username;' || echo "Unable to create user on pizza.cs.fredonia.edu";;

Ubuntu 14.01

share|improve this question
    
Are you using a literal $username? If so, where are you getting that from? Are you expecting the shell to expand it? When I run useradd $username in a local shell I get the help text. –  Seth Sep 17 '14 at 3:25
    
Is the user [email protected] able to run useradd? You typically have to be root or sudo useradd assuming that the user backup has sudo rights. –  slm Sep 17 '14 at 5:06
    
That is what I now need to add, he does not have the rights to adduser –  mrplow911 Sep 17 '14 at 16:10

1 Answer 1

up vote 3 down vote accepted

Have you tried using the double quote? Inside single quotes, BASH will not expand the variable $username.

For example, if $username=bob, then this command will expand the variable:

ssh user@hostname "useradd $username; mkdir /home/$username;" 

the quoted portion will expand to:

useradd bob; mkdir /home/bob;

But if you use single quotes, like this:

ssh user@hostname 'useradd $username; mkdir /home/$username;'

Then the quoted portion remains unchanged. It will be interpreted as:

useradd $username; mkdir /home/$username;

BTW, the ; at the end, after $username, isn't necessary.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.