How can I run a shell script from bashrc file?

33,734

Solution 1

Just create a function:

function masterScript()
{
    if [ -e /home/arun/Desktop/scripts/myMasterScript.sh ]
    then
        bash /home/arun/Desktop/scripts/myMasterScript.sh
    fi
}

And make sure your script is executable:

chmod 755 /home/arun/Desktop/scripts/myMasterScript.sh

Solution 2

I tried this out .. I simply made it

alias masterScript="cd /home/arun/Desktop/scripts && myMasterScript.sh"

Solution 3

Make sure your masterScript.sh is executable by doing:

chmod +x masterScript.sh

If the script is not executable, the bash command cannot run it and you'll get these kind of error messages.

Share:
33,734

Related videos on Youtube

Arun Mohan
Author by

Arun Mohan

Updated on September 18, 2022

Comments

  • Arun Mohan
    Arun Mohan over 1 year

    What I basically want to achieve is to type in a custom command in the terminal and a specific shell script should run each time.

    I could achieve the above requirement with folders,by modifying the bashrc file like below

    alias myScripts="cd /home/arun/Desktop/scripts"
    

    Now when I try to do the same with a bash script by modiying the bashrc file as given below,

    alias masterScript="bash /home/arun/Desktop/scripts/myMasterScript.sh"
    

    now when I type masterScript im getting the following error:

    "bash : No such file or directory" error
    

    How can I correct this?

    • Bruni
      Bruni about 8 years
      I do not understand why this would be preferable to just put your script in your $PATH. If you move your script to ~/bin and add ~/bin to your path variable you can execute it from everywhere.
    • Xen2050
      Xen2050 about 8 years
      Sounds like the problem is/was that /home/arun/Desktop/scripts/myMasterScript.sh just didn't exist - typo?
  • Xen2050
    Xen2050 about 8 years
    This is not true - try it with bash non-executable-file and bash will run it
  • codedge
    codedge about 8 years
    Ah right.. the +x permission seems to only matter if run via ./masterScript.sh
  • John Orion
    John Orion about 8 years
    sorry you are correct .. i updated my answer .. missed the ending quote :(
  • Xen2050
    Xen2050 about 8 years
    a bash terminal will display a newline & > waiting to close an open quote, makes it easy to catch. anyway, i'll del my old comment, and maybe this one tomorrow. +1
  • Sergiy Kolodyazhnyy
    Sergiy Kolodyazhnyy about 8 years
    That's the way i would do it. +1 . There's no need for script , if you can achieve same action in few lines of code. Functions are ideal for that.