I have a number of self-made (bash) scripts that I often use. Here are some examples of these scripts:   A script backup-system.sh that backs up my system to an external hard drive. A script repeat.sh that allows me to repeat a command multiple times. For example, repeat.sh 5 echo "x" prints "x" 5 times. A script switch-sound.sh that switches between outputting sound over my loudspeaker and over my headset.   Because I use these scripts often, I want the ability to easily call them from my command line. For example, I want to type backup-system to run backup-system.sh.  How can I install my self-made scripts to run them from the command-line? Since this is a common issue, I would like to know the standard way to achieve this, that ideally works for many Linux distributions (however, Ubuntu is the priority).    More details:   I am the only user of my computer. Still, I would like to be able to use my scripts as both my own user and as root. I know I could add an entry to .bashrc to achieve some of what I want. But because my scripts are often long, this is impractical.
122073
I need to write a program or a bash script, which will run every now and then (based on cron). The program will contain a state machine and will behave differently depending on which state it is in.  The program (or bash script) is not user specific, so it shouldn't be in the /home folder.  Where (by convention) to store the file with program internal state on Ubuntu Server?  Bonus question: What is the best place for the script itself?
95791
Is there some way I can run a Bash script as root without being prompted for a password every time? I'm attempting to automate the process of starting my LAMPP install, which requires me running a couple of sudo commands to start and stop services. Optimally, I'd like to build this into an executable file, so all I'd have to do is click an icon. ;)  Obviously, I have the password, so that's not an issue. I just want to take my laziness one step farther and not have to enter it every time I start my localhost. After all, that's what programming is all about, right?
60405
I would like to run a script from the main ubuntu shell as a different user that has no password.  I have full sudo privileges, so I tried this:  sudo su -c "Your command right here" -s /bin/sh otheruser   Then I have to enter my password, but I am not sure if that script is now really running under that user.  How can I confirm that the script is really running under that user now?
32307
I preface this by saying I am pretty much a complete noob when it comes to script writing.  Anyway, I am looking to create a script that opens a terminal in the GUI environment of Ubuntu, and when it does, shows the output of the other commands running in the script.   The reason for this is I am looking to take the script, throw it into the directory that runs scripts on log in and have it opening a terminal, run my normal update scripts and then keep the terminal open to allow me to continue after.  My reason is basically to just get more experience, and I figured to automate something I do every time I login.
9166
I have multiple Amazon EC2 accounts and want to quickly be able to switch variables, such as $EC2_HOME, using a script.  I have have a shell script set up like this:  #!/bin/sh export EC2_HOME=/home/me/.ec2 echo $EC2_HOME   When I run the script I know that EC2_HOME is set, but I thought that using export would make the variable stick around after the script completed. It does not, as running echo $EC_HOME does not show anything.  I know this must be very rudimentary Linux scripting knowledge, but I don't know it. I tried looking for related questions without luck - so my apologies if this is a duplicate.
44440
I have a local script that I want to run on several remote servers. The command syntax I'm using is:  ssh &lt;remote_server&gt; "bash -s" &lt; ./local_script.sh   This works fine, and even lets me pass parameters to local_script.sh. However, I'd really like to pass an input file to it, as in:  local_script.sh &lt; local_file.txt   Combining these two statements gives:  ssh &lt;remote_server&gt; "bash -s" &lt; ./local_script.sh &lt; local_file.txt   When I run this, I get  bash: line 1: FOO: command not found bash: line 2: BAR: command not found bash: line 3: BAZ: command not found ...   where FOO, BAR, BAZ, etc. are the first word on each line of local_file.txt:  FOO     FOO_PARAM1 BAR     BAR_PARAM1 BAZ     BAZ_PARAM2 ...   So, it looks like "bash -s" on the remote server is interpreting local_file.txt as a script file, instead of an input file to local_script.sh. Is there any way to fix this (other than creating a wrapper script)?
47205
I'm running a remote bash script (using ssh -t 'bash doscript.sh'). In "doscript.sh" I have a sudo su anotheruser. At that point, the script seems to push a shell and I'm left in that "interactive" script and the rest of my script (doscript.sh) doesn't run unless I type "exit". Is there a workaround that gets around this problem?  Thanks in advance.  Here is the contents of my test script "doscript.sh":  sudo su diy cd pwd whoami exit whoami
36318
ssh can use to run remote commands.  ssh me@server.com 'long-script.sh'   I run a long script that will take a lot of time, but I want to close my computer and keep running the script in the remote server. I know how to achieve this with , but I need do it via ssh.  Can I do that without interrupting my script?
118321
I'm on a Mac but I think this is generally Unix-applicable.  I'm in the process of learning shell scripting and there's something I seem to be missing. When I'm in the ordinary terminal, I can use scripting syntax like for loops and such in conjunction with commands to do stuff.  But....bash opens an interpreter for shell scripting.  Which is where I get confused, because isn't the terminal already an interpreter for shell scripting, as demonstrated by the fact that the scripting works when given to stdin?  Bonus question: how is bash different from bash -i, which according to man "starts an interactive session".....isn't that what happens when you just enter bash on its own? Which, to my eye is no different than being in the normal terminal in the first place...
81080
I'm creating a simple bash script and I want to create a select menu in it, like this:  $./script  echo "Choose your option:"  1) Option 1   2) Option 2   3) Option 3   4) Quit     And according to user's choice, I want different actions to be executed. I'm a bash shell scripting noob, I've searched the web for some answers, but got nothing really concrete.
79332
I seem to misunderstand the Bash rule for creating a subshell. I thought parentheses always creates a subshell, which runs as its own process.  However, this doesn't seem to be the case. In Code Snippet A (below), the second sleep command does not run in a separate shell (as determined by pstree in another terminal). However, in Code Snippet B, the second sleep command does run in a separate shell. The only difference between the snippets is that the second snippet has two commands within the parentheses.  Could somebody please explain the rule for when subshells are created?  CODE SNIPPET A:  sleep 5 ( sleep 5 )   CODE SNIPPET B:  sleep 5 ( x=1 sleep 5 )
55428
I have a bash script myhome.sh containing only one line:  echo $HOME   The script's owner is a user:  $ ls -l myhome.sh -rw-rw-r-- 1 user user &lt;date&gt; &lt;time&gt; myhome.sh   In Ubuntu 16.04 and 17.10 I get:  $ echo $HOME /home/user  $ sudo echo $HOME /home/user  $ bash myhome.sh /home/user  $ sudo bash myhome.sh /home/user   In Debian Buster/Testing I get:  $ echo $HOME /home/user  $ sudo echo $HOME /home/user  $ bash myhome.sh /home/user  # WHY ? $ sudo bash myhome.sh /root   I don't understand why inside the script in Debian, if it's executed with sudo, I always get $HOME=/root while in Ubuntu I get $HOME=/home/user. Does anyone know what have the Ubuntu developers changed?
922
This may be a silly question, but I ask it still. If I have declared a shebang  #!/bin/bash    in the beginning of my_shell_script.sh, so do I always have to invoke this script using bash  [my@comp]$bash my_shell_script.sh   or can I use e.g.  [my@comp]$sh my_shell_script.sh   and my script determines the running shell using the shebang? Is it the same happening with ksh shell? I'm using AIX.
109277
If I am correct, command substitution, process substitution, pipeline, and background jobs run the commands specified within themselves in a subshell not in the original shell.  But when the commands are jobs or dirs, they output exactly the same as when they are run directly in the original shell:  echo $( jobs )  cat &lt;(jobs)  jobs | less  echo $( dirs )  cat &lt;(dirs)  dirs | less  dirs &amp;   Why is that? Is it because the subshells inherit the jobs and dirs stack from the original shell, or because jobs and dirs are run in the original shell instead of the subshells?  But   jobs &amp;   doesn't output anything. Why is it different from the other commands? Thanks.  Related
6219
I'd like to run a .sh file using bash on logout and shutdown of Ubuntu. I have searched up and down the web and have found only fixes for KDE and GNOME. The script is simple, I just need to know if I can add the line of code to the "logoff/shutdown" file or if I need to reference it in another script.
21443
Is there any standard that covers the portability of running a command after variable assignment on the same line?  APPLE="cider" echo hi   How portable is something like that? Where will it work and where won't it?   Also: my shell scripts start with #!/bin/sh if that makes any difference.
104419
I have a script myscript.sh  #!/bin/sh  echo $1 $2   which is used something like ...  ./myscript.sh foo bar   which gives me an output of ...  foo bar     but how can i make this script include custom command options? for example ...  ./myscript.sh -a foo and corespond to $1   and  ./myscript.sh -b bar and corespond to $2