Linux and Command-Line Basics

$ X=1
$ bash -c 'echo "[$X]"'
$ export X
$ bash -c 'echo "[$X]"'

When the commands above are run in order, which of the following is the pair of outputs of the two bash -c commands?

  1. A[] and [1]
  2. B[1] and [1]
  3. C[] and []
Show answer and explanation

Answer: A. [] and [1]

A variable that is only assigned, as in "X=1", is a shell variable: it can be used only inside that shell and is not passed to child processes it starts (here a new bash). With export it becomes an environment variable, and its value is inherited by child processes started afterwards. Therefore the first output is [] and the second [1]. Because of the single quotes, $X is expanded not in the parent shell but inside the child bash.

  • A. Correct. Until it is exported, X is not passed to child processes.
  • B. A shell variable that is only assigned is not inherited by child processes.
  • C. Child processes started after export inherit the value of X.
Practice this topic Back to topic

Books on Linux and Command-Line Basics Ad

As an Amazon Associate I earn from qualifying purchases.

More Linux and Command-Line Basics questions

  1. Which of the following is the main function of a Linux package management system (apt on Debian-based systems, dnf on Red Hat-based systems and so on)?
  2. On a Linux system using systemd, which of the following is the difference between "systemctl start license.service" and "systemctl enable license.service" for the service license.service?
  3. The line "30 2 * * 1 /home/alice/backup.sh" was registered in cron. Which of the following is when backup.sh is run?
  4. A Linux server warns that disk space is running low. Which of the following is the correct pair of a command to check usage and free space for each file system and a command to total the space used by the files in a directory?
  5. You created a public-key authentication key pair with ssh-keygen on your own PC and want to connect to the server server1 with ssh without a password. Which of the following is the correct way to place the keys?