Thread: *** how to set up a variable in Linux? ***

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    *** how to set up a variable in Linux? ***

    I am a newbie. I know this is a little off-stream of programming, but it is the problem when I tried to program in linux. I wonder if there is a way to set up a variable, say $VAR = /home/root, so that I can use
    cd $VAR
    instead of
    cd /home/root

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    VAR=/home/root

    Where you put it depends on various factors. The shell startup scrips are a good place. (In the case of bash they are .bashrc, .bash_profile and so on, in your home directory.)

    But this specific example (home) is better solved by just doing
    cd
    and the variable HOME is already set to your home directory anyway.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by CornedBee View Post
    VAR=/home/root
    Also, don't put spaces on either side of the equals sign. It won't work if you do that.

    Where you put it depends on various factors. The shell startup scrips are a good place. (In the case of bash they are .bashrc, .bash_profile and so on, in your home directory.)
    I typically put such things in .bashrc, because that is always parsed even by non-login shells. Commonly, xterm or other terminal emulators launch their shells as NON-login shells. Such shells do not process the profile, but instead depend on the profile variables already set by the parent shell.

    What it boils down to is that changes you make in .profile or .bash_profile may not be visible until you log out of your X session and log back in. So... I always put stuff in .bashrc instead.

    EDIT: And of course, you don't have to put variable definitions in EITHER location. You can do it directly at a command line, if you want.
    Last edited by brewbuck; 10-05-2007 at 03:12 PM.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thanks all. But what are the differences among .bashrc, .bashrc_profile, and .profile ?

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Got the answer:

    startup files

    These files contain the aliases and environmental variables made available to Bash running as a user shell and to all Bash scripts invoked after system initialization.
    /etc/profile

    systemwide defaults, mostly setting the environment (all Bourne-type shells, not just Bash [1])
    /etc/bashrc

    systemwide functions and aliases for Bash
    $HOME/.bash_profile

    user-specific Bash environmental default settings, found in each user's home directory (the local counterpart to /etc/profile)
    $HOME/.bashrc

    user-specific Bash init file, found in each user's home directory (the local counterpart to /etc/bashrc). Only interactive shells and user scripts read this file. See Appendix G for a sample .bashrc file.

    logout file

    $HOME/.bash_logout

    user-specific instruction file, found in each user's home directory. Upon exit from a login (Bash) shell, the commands in this file execute.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You may want to do "export var=value", as that will make the variable available for other processes too.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  3. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  4. Trying to set a max for my variable...
    By GameGenie in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2005, 06:55 PM