0

When reading Bash Reference Manual in shell operation, it says:

  1. Reads its input from a file (see Shell Scripts), from a string supplied as an argument to the -c invocation option (see Invoking Bash), or from the user’s terminal.

Here are my questions:

  1. what is the difference of the 3 ways of reading input?
  2. can a script have arguments? like $ ./script arg1 arg2 ...

And when I type some command one the terminal like:

$ command1 arg1 arg2 ...

How does Bash know command1 is a executable (no further processing, just execute it) not a script (to read the content of the script)?

1
  • 2
    Where's Tim when you need him?
    – Jeff Schaller
    Commented Feb 26, 2017 at 16:33

1 Answer 1

1
  1. The main difference is that when reading from the terminal, the shell displays prompts. Otherwise, there's no difference

    $ echo ls > script
    $ bash script
    script
    $ bash -c ls
    script
    $ ls
    script
    

    The shell knows whether it's running interactively, and some commands might behave differently based on that, too.

  2. Yes, it's a common thing. arguments to a string fill its positional parameters. Search for $@ and $1, $2 etc. in man bash.

    See ARGUMENTS, FUNCTIONS, and ALIASES in man bash. In brief, when you run command1 arg1 arg2, bash first tries to expand the alias command1 and run the result, or to run a function command1, and if even that isn't possible, it tries to find the command in its $PATH.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .