FreeBSD csh / tcsh – Export Shell Variable

I am using FreeBSD version 7.x/8.x/9.x/10.x/11.x/12.x with csh (c shell) or tcsh. How do I export tcsh/csh shell variable under FreeBSD operating systems?

The tcsh is an enhanced but completely compatible version of the Berkeley UNIX C shell, csh. It is a command language interpreter usable both as an interactive login shell and a shell script command processor. It includes a command-line editor and many other features. This page shows how to export csh or tcsh variable using the command line option on Linux or Unix-like operating systems.

FreeBSD display current environment variables

Type the following command to print current names and values of environment variables under tcsh or csh:
setenv
Sample output:

SHELL=/usr/local/bin/bash
TERM=xterm
SSH_CLIENT=10.10.29.66 37484 22
SSH_TTY=/dev/ttyp2
USER=root
PAGER=more
FTP_PASSIVE_MODE=YES
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin
MAIL=/var/mail/root
BLOCKSIZE=K
PWD=/root
SHLVL=2
HOME=/root
LOGNAME=root
SSH_CONNECTION=10.10.29.66 37484 10.24.116.2 22
_=/bin/csh
HOSTTYPE=FreeBSD
VENDOR=unknown
OSTYPE=FreeBSD
MACHTYPE=unknown
GROUP=wheel
HOST=vps.nixcraft.in
REMOTEHOST=10.10.29.66
EDITOR=vim

Export shell variable

To export and set new environment variables, enter:
setenv name value
setenv EDITOR /usr/bin/vim

You need to add all your environment variables to ~/.cshrc file – csh resource script, read at beginning of execution by each shell. Here is my sample .cshrc file:

alias h         history 25
alias j         jobs -l
alias la        ls -a
alias lf        ls -FA
alias ll        ls -lA

# A righteous umask
umask 22

set path = (/sbin /bin /usr/sbin /usr/bin /usr/games /usr/local/sbin /usr/local/bin $HOME/bin)

setenv  EDITOR  vim
setenv  PAGER   less
setenv  BLOCKSIZE       M

if ($?prompt) then
        # An interactive shell -- set some stuff up
        set prompt = "`/bin/hostname -s`# "
        set filec
        set history = 100
        set savehist = 100
        set mail = (/var/mail/$USER)
        if ( $?tcsh ) then
                bindkey "^W" backward-delete-word
                bindkey -k up history-search-backward
                bindkey -k down history-search-forward
        endif
endif

A list of commonly used tcsh environment variables

An array of strings called the environment is made available by execve() call when a process begins. By convention these strings have the form name=value. The following names are used by various commands

  • BLOCKSIZE : The size of the block units used by several commands, most notably df, du and ls. BLOCKSIZE may be specified in units of a byte by specifying a number, in units of a kilobyte by specifying a number followed by K or k, its of a megabyte by specifying a number followed by M or m etc.
  • COLUMNS : The user’s preferred width in column positions for the terminal. Utilities such as ls and who use this to format output into columns.
  • EDITOR : Default editor name.
  • EXINIT : A startup list of commands read by ex and vi.
  • HOME : A user’s login directory, set by login from the password file /etc/passwd.
  • LANG : This variable configures all programs which use setlocale to use the specified locale unless the LC_* variables are set.
  • MAIL : The location of the user’s mailbox instead of the default in /var/mail, used by mail, sh, and many other mail clients.
  • PAGER : Default paginator program. The program specified by this variable is used by mail, man, ftp, etc, to display information which is longer than the current display.
  • PATH : The sequence of directories, separated by colons, searched by csh, sh, system, execvp, etc, when looking for an executable file. PATH is set to /usr/bin:/bin initially by login.
  • PRINTER : The name of the default printer to be used by lpr, lpq, and lprm.
  • PWD : The current directory pathname.
  • SHELL : The full pathname of the user’s login shell.
  • TERM : The kind of terminal for which output is to be prepared. This information is used by commands, such as nroff or plot which may exploit special terminal capabilities.
  • TMPDIR : The directory in which to store temporary files. Most applications use either /tmp or var/tmp. Setting this variable will make them use another directory.
  • TZ : The timezone to use when displaying dates.
  • USER : The login name of the user.

tcsh export variable

The syntax is as follows to define a new tcsh or csh variable named life with 42 as value:
setenv life 42
To print value of the $life use printf command/echo command:
echo "$life"
printf "%d\n" $life

Now start a new tcsh session (children tcsh session):
tcsh
Find out if children inherit environment variable named $life:
env | grep life
echo "$life"

A list of csh export variable commands

  1. env OR env | grep var_name – Set environment and execute command, or print environment variables
  2. setenv name value – Sets the environment variable name to value and export it too
  3. echo "$var_name" OR printf "%FORMAT" $var_name – Display value of the environment variable name

Further readings:

  • man page – csh, tcsh
🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
3 comments… add one
  • David Mackintosh Sep 2, 2008 @ 15:48

    Might interest you to know that when I read your posts in my RSS Google Reader, I always get a pop-up demanding some credentials.

    See: http://wiki.xdroop.com/gallery2/v/Random/0809/20080902-1140001.jpg.html

  • Rabindra Nayak Aug 25, 2010 @ 16:17

    if ($?prompt) then
    set prompt = “`/bin/hostname -s`# ”
    What does “($?prompt)” indicate in the code?

  • systemBuilder Jun 29, 2012 @ 23:06

    The ($?prompt) term queries the shell, asking if the variable $prompt is set or not.

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.