Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
hello there If have come across three ways of executing a script Code: bash script . script ./script So what is the difference in the above 3 methods Also if ...
  1. #1
    Linux User vickey_20's Avatar
    Join Date
    Mar 2009
    Location
    Mumbai, India
    Posts
    493

    different ways of executing a script

    hello there
    If have come across three ways of executing a script
    Code:
    bash script
    . script
    ./script
    So what is the difference in the above 3 methods
    Also if you know some other method of executing script please add in.
    Only if I could understand the man pages
    Registered Linux user #492640
    OS: RHEL4,5 ,RH 9,Ubuntu

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    Quote Originally Posted by vickey_20 View Post
    hello there
    If have come across three ways of executing a script
    Code:
    bash script
    . script
    ./script
    So what is the difference in the above 3 methods
    Also if you know some other method of executing script please add in.
    "bash script" is an instruction to run bash, passing the script to bash as an argument. Any bash script can be run like this.

    The second method is called "sourcing" the script and is used to incorporate one script within another. The script is run as if it was part of the script that sourced it.

    Neither of these methods requires the script to be executable. The third method, which is the commonest way of running a script from the command line, does require the script to be executable (i.e. the execute bit must be set) and to start with the line
    Code:
    #! /bin/bash
    . This "hash-bang" line tells Linux that bash is the program which must be used to read and execute the script. The initial dot and slash in the command simply means "you will find the script I mean in this directory".
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  3. #3
    Just Joined!
    Join Date
    Jul 2009
    Posts
    3
    Okay, what do I have to do to run a script without putting the ./ in front of it?

    I was trying to use a program that uses spidermonkey and everything is dependent upon just typing "js" at a command prompt. I have to type: ./js on my Debian box.

    What gives?

    Or is it assumed that if the author states: type "js" at the command prompt... that what is really meant is to type in "./js".

    Can you explain - please?

  4. #4
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    that means that it assumes that it is on your path

    most applications will get installed to /usr/bin or /usr/local/bin which will be on the path of the user, so when you type that command, the shell looks for the application in the directories on your path, and then executes that

  5. #5
    Just Joined!
    Join Date
    Jul 2009
    Posts
    3
    So if I have an application installed in /usr/src/js/src and the command from there is ./js can I make a link (ln) in the /usr/local/bin to that command and it will work?

    Thank you for your reply.

  6. #6
    Linux Engineer Kieren's Avatar
    Join Date
    Aug 2007
    Location
    England
    Posts
    845
    Yes, as long as /usr/local/bin is in your path. You can check you path by doing a:

    Code:
    echo $PATH
    This will output a list of different paths seperated by the : symbol
    Linux User #453176

  7. #7
    Just Joined!
    Join Date
    Jul 2009
    Posts
    3
    That did it! Thank you.

    Today is a good day. I've learned something new and it's only 8:04am.

    Thank you again.

  8. #8
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    There's another possibility for this kind of thing which doesn't require you to mess around with system directories. If you create a bin subdirectory in your home directory and put scripts in there, they will execute as simple commands because ~/bin is on your command path in most distros.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  9. #9
    Linux User vickey_20's Avatar
    Join Date
    Mar 2009
    Location
    Mumbai, India
    Posts
    493
    ~/bin is not there in the path by default , it is only /bin
    Only if I could understand the man pages
    Registered Linux user #492640
    OS: RHEL4,5 ,RH 9,Ubuntu

  10. #10
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    Quote Originally Posted by vickey_20 View Post
    ~/bin is not there in the path by default , it is only /bin
    You can add it by putting the following line in your .bashrc file:
    PATH=$PATH:~/bin
    "I'm just a little old lady; don't try to dazzle me with jargon!"

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...