Results 1 to 6 of 6

Thread: Bash Scripting

  1. #1
    Join Date
    Mar 2013
    Beans
    3

    Question Bash Scripting

    Hi!

    i'm new to Linux,

    i'm trying to a simple script like a batch files(ms-dos) and i have no succes and i don't understand why not work.

    i.ve read all tutorial on linuxcommand.org


    here is my problem


    #!/bin/bash
    #nothing here
    echo "Hello"
    Alias cls=clear
    echo "world"

    echo 1 and 2 work,but alias is not executed ,alias cls=clear work on command line,not in the script.

    shell permission on my files is ok

    no command "cls" found is the result


    i have read tutorial many times


    thanks in advance

  2. #2
    Join Date
    Apr 2008
    Location
    LOCATION=/dev/random
    Beans
    5,767
    Distro
    Ubuntu Development Release

    Re: Bash Scripting Problem help wanted

    When you launch a script, it runs in a new session. The alias command is being executed but it's only valid for the current session, ie during the script.

    When the script finishes executing, the session it was running in is closed and you are returned to the session you were in before the script was executed, so the alias is no longer valid.

    If you want to define aliases for all of your terminal sessions you have 2 options. The first is to edit your ~/.bashrc file and define the aliases there. The second option (which I use) is to create the file ~/.bash_aliases and define all of your aliases there, The default ~/.bashrc file provided with Ubuntu (and most other distros) will check for the existence of a ~/.bash_aliases file and source all of its contents if it exists. For example my ~/.bash_aliases file looks like this...
    Code:
    rob@raring:~$ cat ~/.bash_aliases 
    alias sudo='sudo '
    alias ipl='get_iplayer --nocopyright'
    alias gipl='get_iplayer --nocopyright --output=/home/rob/Videos --tvmode=flashhd,flashvhigh,flashhigh,flashstd,flashnormal --get'
    alias hist='history | grep'
    alias sv='sudo vi'
    alias df='df -h -t ext4'
    alias du='du -h --max-depth=1 | sort -hr'
    alias ls='ls -lh --color=auto'
    alias ..='cd ..'
    alias ...='cd ../..'
    alias ....='cd ../../..'
    alias pg='ps -Af | grep $1'
    Cheesemill

  3. #3
    Join Date
    Mar 2013
    Beans
    3

    Thumbs up Re: Bash Scripting

    Thank you Very Much!!!!!

    Now i understand!!!!

    Thank you again

  4. #4
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,791
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: Bash Scripting

    Quote Originally Posted by 2001cpx View Post
    Thank you Very Much!!!!!

    Now i understand!!!!

    Thank you again
    The above remarks also hold when you set environment variables. However, you can also execute a script in the context of your current session by calling it with "source" or ".", i.e.:
    Code:
    source my_settings
    # or
    . my_settings
    in which case environment variables and aliases will apply to the current session.

    This also lets you define functions/variables common to several scripts in a single file

  5. #5
    Join Date
    Mar 2013
    Beans
    3

    Re: Bash Scripting

    I tried your command and is very interesting!!

    work like a batch files (i have 49 years old....and liked in the past MS-dos)

    i like a powerfull linux command.

    Now i can start to take a control of my Machine! (need surely 5 or 10 years...)

    thanks for your precious help
    Last edited by 2001cpx; March 20th, 2013 at 12:54 AM.

  6. #6
    Join Date
    Nov 2008
    Location
    Metro Boston
    Beans
    8,519
    Distro
    Kubuntu 14.04 Trusty Tahr

    Re: Bash Scripting

    Remember that bash, like all things *nix, is case-sensitive. "Alias" will not work because the command is named "alias." Except in some very rare cases, almost every command in Linux and other Unix variants uses lower case.
    If you ask for help, please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

Bookmarks

Posting Permissions

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