Thread: Linux Shell Script $@

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    Linux Shell Script $@

    I'm looking through some source at sourceforge and I came across a batch script that was using "$@" and I cant find out what it's referencing since you can't google symbols like that. I'm sure it's something basic. If someone can enlighten me, i'd appreciate it.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Similar but not identical to $*
    Code:
    #!/bin/bash
    
    test_func() {
    	for x in $@; do
    		echo $x;
    	done;
    }
    
    test_func this that etc
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    32
    Quote Originally Posted by Verneg View Post
    I'm looking through some source at sourceforge and I came across a batch script that was using "$@" and I cant find out what it's referencing since you can't google symbols like that. I'm sure it's something basic. If someone can enlighten me, i'd appreciate it.
    man bash

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Quote Originally Posted by Valery Reznic View Post
    man bash
    Bash for shell scripts is evil thing.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    32
    Quote Originally Posted by fronty View Post
    Bash for shell scripts is evil thing.
    Why do you think bash is evil ? I like it.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Quote Originally Posted by Valery Reznic View Post
    Why do you think bash is evil ? I like it.
    Bash for interactive use isn't evil (I don't like it in that use either, but it doesn't matter here). If you use bash, people using systems which don't come with bash have to modify at least shebang. If you use some bash-isms, users of other shells have to modify your code too. If you use plain /bin/sh, it will work in every system.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    32
    Quote Originally Posted by fronty View Post
    Bash for interactive use isn't evil (I don't like it in that use either, but it doesn't matter here). If you use bash, people using systems which don't come with bash have to modify at least shebang. If you use some bash-isms, users of other shells have to modify your code too. If you use plain /bin/sh, it will work in every system.
    If you put as shebang #!/bin/bash they should modify nothing, save may be installing bash.
    But same is hold for python, perl, ruby , awk - any interpreted language.

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Quote Originally Posted by Valery Reznic View Post
    If you put as shebang #!/bin/bash they should modify nothing, save may be installing bash.
    Why would you put /bin/bash if you don't use any features which depend on bash? And I think that is even worse like using some other thing that binds the code to some platform, if it could be implemented with standard features which would make it usable without modifications on other platforms. I've seen too much code which relys on some stupid linuxism, because too many programmers think any unixish system == linux. And why shouldn't they modify anything, even hashbang to make it usable on their system? Forcing people to install some shell would be rediculous. And if I install something, hell no I would put anything in /bin or anything directly under root. /usr/local or possibly /opt is for installed programs.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    /bin/sh is more generic, but the thread is called "Linux Shell Script $@", and the linux shell is bash, and it is in /bin. If not, you should have a softlink in bin called bash.

    Vis, bashism, arrays are a bashism, so I'm not even sure basic C-shell syntax includes $@ (but maybe it does). In any case, if you are scripting for bash you might as well put "/bin/bash" at the top so there will be no confusion there. Otherwise, you are going to have to do a lot of work and testing trying to figure what is truly generic and what isn't (I don't think there is a formal standard in place here). You do not have to learn about every shell in order to script for one of them.

    It seems much more reasonable, if you want to port a bash script to a non-bash system, for you to do the conversion, rather than expect bash scripters to write for you. The fact that bash includes c shell syntax and hence sh scripts are common and work on linux systems does not mean that the same thing has to be true the other way around -- otherwise there would be no point in a bash shell.
    Last edited by MK27; 04-09-2010 at 02:09 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    This is a matter of preference and that's your opinion which I most likely can't change here.

    By the way, csh and sh are very different thing and incompatible on VERY large parts.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by fronty View Post
    By the way, csh and sh are very different thing and incompatible on VERY large parts.
    You're right, "sh" refers to the original Bourne shell, which I thot was 100% derived from the C-shell (which I've never used). But it isn't, so some of what I wrote is a little mixed up. Your contention makes a little bit more sense too, but I still don't think people who script for bash should be expected to learn the difference if they never actually use the original sh -- they you are just asking for trouble from apparently portable scripts.

    Unix shell - Wikipedia, the free encyclopedia
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deallocating a struct containing arrays
    By maxtothemax in forum C Programming
    Replies: 5
    Last Post: 09-28-2009, 07:09 PM
  2. Thinking of upgrading to linux...
    By Yarin in forum General Discussions
    Replies: 37
    Last Post: 07-24-2009, 11:40 AM
  3. Extended ASCII/ANSI in Linux Shell
    By JupiterV2 in forum Linux Programming
    Replies: 5
    Last Post: 10-12-2007, 02:38 PM
  4. Linux Shell and mp3
    By afreedboy in forum Linux Programming
    Replies: 2
    Last Post: 06-24-2004, 12:08 PM
  5. Shell script
    By diamond in forum Linux Programming
    Replies: 5
    Last Post: 03-26-2004, 08:39 AM