Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, Iam not able to use aliasas in my script. below alias "ll" is for "ls -l"....is working fine on command promt, but not if Iam using the same in ...
  1. #1
    Just Joined!
    Join Date
    Jun 2006
    Posts
    40

    Iam not able to use aliases in my script...

    Hi,
    Iam not able to use aliasas in my script. below alias "ll" is for "ls -l"....is working fine on command promt, but not if Iam using the same in my script.

    Please have a look at below output....Kindly help me, why it is not giving me the output.


    [pawan@gfmdatabase pawan]$ ll | tail
    -rw-rw-r-- 1 pawan pawan 1152 Mar 13 2006 uptolow.sh
    -rw-rw-r-- 1 pawan pawan 550 Mar 6 2006 usecase.sh
    -rw-rw-r-- 1 pawan pawan 635 Mar 13 2006 usefunc.sh
    -rw-rw-r-- 1 pawan pawan 120 Oct 18 2006 usewhile.sh
    -rw-rw-r-- 1 pawan pawan 43 Apr 4 2006 uuu.sh
    -rw-rw-r-- 1 pawan pawan 274 Mar 29 2006 vowel.pl
    -rw-rw-r-- 1 pawan pawan 242 Mar 16 2006 whileuse.pl
    -rw-rw-r-- 1 pawan pawan 273 Mar 21 2006 wrdcnt.pl
    -rw-rw-r-- 1 pawan pawan 957 Mar 21 2006 wrdsrch.pl
    -rw-rw-r-- 1 pawan pawan 339 Feb 13 2006 xyz
    [pawan@gfmdbase pawan]$ cat aks.sh
    ll | tail
    [pawan@gfmdbase pawan]$ ll aks.sh
    -rwxr-xr-x 1 pawan pawan 10 May 18 16:43 aks.sh
    [pawan@gfmdbase pawan]$ ./aks.sh
    ./aks.sh: line 1: ll: command not found

    [pawan@gfmdbase pawan]$


    Please share, if anyone have a idea to get this working in a simple bash script.

    Thanks
    Pawan Sangal

  2. #2
    Just Joined! Sivel's Avatar
    Join Date
    Jun 2005
    Location
    Maryland
    Posts
    20
    There are inherent problems with using aliases in bash scripts. I suggest just using the full command. If the reason you want to use the alias is because you are having issues executing the command because it has spaces then include the full command inside of $()

    Example: $(ls -l)

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    The bash manual page suggests using functions instead of aliases. Aliases are not exported, so you need to arrange to place those in a file, and then you tell bash to load that file for non-interactive shells. This becomes complicated because bash tries to perform actions that useful for you, that conform to older shells like sh, and to conform to POSIX standards.

    Here is a a script that illustrates how you could get aliases into child processes:
    Code:
    #!/bin/bash
    
    # @(#) s2       Demonstrate (non)-inheritance of aliases in bash.
    
    echo " bash version: $BASH_VERSION"
    
    echo
    echo " This is process $$, main script."
    echo " Aliases are:"
    alias
    
    echo
    echo " Define an alias, and print list of aliases"
    alias e=vi
    alias
    
    echo
    bash <<'EOF'
    echo " This is process $$, subordinate script."
    echo " Aliases are:"
    alias
    EOF
    
    echo
    echo " Place an alias in an rc file:"
    export BASH_ENV=t1
    cat >t1 <<EOF
    alias my-editor=vim
    EOF
    
    echo
    echo " Current aliases:"
    alias
    
    echo
    bash <<'EOF'
    echo " This is process $$, subordinate script."
    echo " Aliases are:"
    alias
    EOF
    
    exit 0
    Which produces:
    Code:
    $ ./s2
     bash version: 2.05b.0(1)-release
    
     This is process 6130, main script.
     Aliases are:
    
     Define an alias, and print list of aliases
    alias e='vi'
    
     This is process 6131, subordinate script.
     Aliases are:
    
     Place an alias in an rc file:
    
     Current aliases:
    alias e='vi'
    
     This is process 6133, subordinate script.
     Aliases are:
    alias my-editor='vim'
    See man bash for details on files that bash reads for new processes and for aliases and functions. Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

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