setenv Command Tutorial To Add, Delete and Change Environment Variables In Linux


Linux and Unix ecosystem mainly used command line based. While working with command line and C Shell we generally need some values to use with commands. Shells provide environment variables for this. This environment variables can be managed with setenv command like add, change and remove.

Syntax

Syntax of setenv command is very simple we just need to provide the variable name and data

setenv VARIABLENAME DATA

List All Environment Variables

We can use setenv in order to list all environment variables currently defined in C Shell.

$ setenv
List All Environment Variables
List All Environment Variables

Add Environment Variable

We will start by creating come environment variable and setting some data to it. We can use lowercase or uppercase letters bu the general usage is uppercase letters. In this case we will create a variable named MYIP and set value 192.168.1.10

$ setenv MYIP 192.168.1.10

Print Environment Variable

We can print specific environment variable with echo command. We will provide the environment variable and and prefix with $ . In this example we will print environment variable named MYIP like below.

$ echo $MYIP
Print Environment Variable
Print Environment Variable

Pass Environment Variables To Sub or Child Shell

Linux process and shell architecture provides the ability to run sub process or shell. Sub process and shell will create a new environment. If we need to use current environment variables in the sub process or shell we just need to use them accordingly. For example in the following example we will print previously defined MYIP in the bash sub-shell.

$ echo $MYIP
Pass Environment Variables To Sub or Child Shell
Pass Environment Variables To Sub or Child Shell

Set OR Update One Environment Variable Value

After assigning a value to a environment variable we may need to update it with new value. We can use set command in order to update current environment variable with a new value. In this example we will update our variable MYIP with 192.168.1.20

$ set MYIP=192.168.1.20
Set OR Update One Environment Variable Value 
Set OR Update One Environment Variable Value 

Useful Environment Variables

There are a lot of default enviroment variables. Here are some of them.

  • USER stores current user name
  • PWD current working directory
  • SSH_CLIENT the ssh client IP address with source and destination port numbers
  • SHELL current shell
LEARN MORE  How To Bash Concatenate or Add Strings?

Leave a Comment