Home > shell > How can I execute a shell script that exists in a longish path with a single command without first cd'ing to the directory?

How can I execute a shell script that exists in a longish path with a single command without first cd'ing to the directory?

July 31Hits:1
Advertisement

In Linux I always cd to a longish path and then run the script:

cd /scratch/someDir/someOthernestedDir/ ./shellscriptName.sh  

How can I avoid achieve typing this longish path and then executing the command with a single step?

Some thing like the below from any path should do what I want:

executeMyCommand 

P.S: I am using C-shell.

[[email protected] bin]$ echo $0 csh 

Answers

There are three main ways of running your script without needing to specify the full path.

  1. Add the directory containing your script to your $PATH. You will then be able to execute the script by name from any directory, just like any other program. If you are using csh, add this to your ~/.cshrc:
    set path = ($path /scratch/someDir/someOthernestedDir/)
    
    
  2. Place a link to your script in a directory that is already in your path. For example /usr/bin:
    ln -s /scratch/someDir/someOthernestedDir/shellscriptName.sh /usr/bin
    
    
  3. Make an alias as @EightBitTony suggested, add this line to your ~/.cshrc:
    alias executeMyCommand '/scratch/someDir/someOthernestedDir/shellscriptName.sh'
    
    

Command aliases.

alias name definition

e.g.

alias executeMyCommand '/scratch/someDir/someOthernestedDir/shellscriptName.sh'

From here.

Adding a command alias

To make a command alias a permanent part of your working environment:

  1. Edit the shell startup file .cshrc and add the line defining the alias.
  2. Save the changes and leave the editor.
  3. Source the shell startup file to have the changes take immediate effect. For example:

    source .cshrc

Related Articles

  • How can I execute a shell script that exists in a longish path with a single command without first cd'ing to the directory?July 31

    In Linux I always cd to a longish path and then run the script: cd /scratch/someDir/someOthernestedDir/ ./shellscriptName.sh How can I avoid achieve typing this longish path and then executing the command with a single step? Some thing like the below

  • Is there any disadvantage of executing a shell script using nohup?March 27

    To execute a shell script in background and without an interruption, we use nohup command. For example: $nohup mytest.sh & Press ALT Press Enter Is there any drawback or disadvantage of this nohup utility as this is not a UNIX / LINUX inbuilt command

  • Execute a shell script when growl notification is shownApril 29

    How do you execute a shell script whenever a Growl notification is shown? And make it application specific. For instance, run a the shell script say "You have new mail" whenever a growlMail notification is shown? --------------Solutions---------

  • logrotate is not executing custom shell script in postrotateJuly 31

    I have following logrotate configuration file /home/application/*/shared/log/*.log { daily rotate 10 missingok nocompress notifempty copytruncate sharedscripts postrotate echo "Hi....." /home/application/test.sh > /home/application/test_bash.

  • executing a shell-script through makefileJanuary 10

    i am trying to create a .deb file that first installs the dependency that is stored in the some folder, for that i have created a shell script. My issue is executing the shell script through make file my make file format to call a shell script is lik

  • Can we execute a shell script by clicking

    Can we execute a shell script by clicking September 9

    This question already has an answer here: Bash script with execution permissions won't run with dbl click 2 answers Unable to run executable link 1 answer Is it possible to double click and execute a shell script instead of executing the following ".

  • Executing all shell scripts in specified directoryNovember 25

    Hi, I need to write a shell script that would: -check if a specified directory exists and contains any files -chmod and chown all files inside the directory -execute all shell scripts in that directory (*.sh) That script would be run by Cron using ro

  • Executing bash shell scriptsMarch 13

    I am reading an article on executing bash shell scripts. Method 1: Create a directory for your shell scripts and add the directory to the contents of the PATH variable, so that you can execute the script as shown below. When this executes, the variab

  • Execute a shell script only for LiveCD session

    Execute a shell script only for LiveCD session February 28

    I want to execute a shell script on startup but only for LiveCD session not for harddisk installation. Which directory should I put my shell script? For example Ubuntu somehow detecting LiveCD session and putting these icons to desktop: -------------

  • How can I execute a shell script when someone changes a file through FTP?August 3

    I'm running Ubuntu 15.04 Server on a VPS. I'm trying to make it so that my friend can change a configuration file for a game server through FTP, and then when that's done execute a shell script that kills the server and then restarts it. Thanks! ----

  • How do I login to another UNIX machine and execute a shell script stored there? August 5

    How do I login to another UNIX machine and execute a shell script stored there? --------------Solutions------------- From machine1, you can login to machine2 and execute a shell script there using SSH: ssh [email protected] /home/myusername/myscrip

  • How to avoid the ./ when executing a shell script?July 11

    I used to know a way to run my shell scripts just by typing there name as long as there were in my home folder or desktop. I hate having to type ./myscript.sh. I would like to be able to type just myscript.sh. How can I fix my terminal to do this? I'

  • How to execute a shell script with selected files/folders in finder?

    How to execute a shell script with selected files/folders in finder?June 20

    I'm coming from Linux where it's easy to run a script against a set of files in nautilus or dolphin, but I was kind of surprised to see that this isn't easy in OSX. Preferably, I would love to be able to ctrl-click on some selection in finder, choose

  • Different ways to execute a shell scriptOctober 11

    There a several ways to execute a script, the ones I know are: /path/to/script # using the path (absolute or relative) . script # using the . (dot) source script # using the `source` command Are the more of this? What are the differences between them

  • Allow user to execute a shell script without seeing its contents?February 18

    I'd like to have an hg hook that sends email using a gmail account. Obviously I don't want anyone to be able read the email-sending script except me or root, since it has a password in, so here's what I've tried: -rwsr-xr-x 1 james james 58 Feb 18 12

  • Return last command executed in shell-scriptJuly 25

    In a bash-script I'd like to retrieve the last command which was executed. In bash itself the following command works like a charm. lastCommand=$(echo `history |tail -n2 |head -n1` | sed 's/[0-9]* //') Inside scripts however history doesn't work at a

  • Executing a shell script from remote server on local machineJanuary 19

    Imagine a shell script on the remote server as #!/bin/bash rm /test.x How can I (if possible) to execute this script from my local machine to delete /test.x file on my local machine. Obviously, the solution should be with something like ssh authoriza

  • which shell will sudo use to execute a shell script without the shebang lineSeptember 27

    My environment is Ubuntu 12.04 LTS, and the sudo version is 1.8.3p1. First I login as a normal user: $ whoami fin $ cat /etc/passwd | grep -i "root\|fin" root:x:0:0:root:/root:/bin/bash fin:x:1000:1000:This is a normal user:/home/fin:/bin/bash $

  • execute a shell script when USB is connected October 23

    This question already has an answer here: Autorun a script after I plugged or unplugged a USB device 2 answers I am working on an application which deals with all kinds of USB storage devices(such as taking backup, updating DataLogFile etc) My proble

  • Commands not executing in shell script run by udevOctober 13

    I'm having trouble using udev to run a shell script that backs up my hard drives to an external drive whenever the external drive is connected via USB. Everything works except the actual call to rdiff-backup. The udev detects the hard drive and calls

Copyright (C) 2018 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.849 s.