Environment variables are not working for non-interactive shells.

Solution Unverified - Updated -

Issue

  • Defining environment variables in ~/.kshrc for a user with ksh as the login shell works on an interactive shell:

    [root@server ~]# su - user
    Last login: Fri Dec 20 11:08:13 CET 2024 on pts/1
    $ whoami
    user
    $ echo $SHELL
    /usr/bin/ksh
    $ tail -n2 ~/.kshrc    
    # User specific aliases and functions
    export VAR=/opt
    $ echo $VAR
    /opt
    $ 
    
  • However the same fails when using su - <USER> -c instead:

    [root@server ~]# su - svchpsuat -c "echo $VAR"
    
    [root@server ~]# 
    
  • The same behaviour applies to bash and ~/.bashrc

Environment

  • Red Hat Enterprise Linux 7
  • Red Hat Enterprise Linux 8
  • Red Hat Enterprise Linux 9
  • ksh
  • bash

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content