Why does bash not keeping value of $_ if bash-completion is used in Red Hat Enterprise Linux 8/7?

Solution Unverified - Updated -

Environment

  • Red Hat Enterprise Linux 8
    • bash-4.4.19-6.el8.x86_64
    • bash-completion-2.7-5.el8.noarch
  • Red Hat Enterprise Linux 7
    • bash-4.2.46-28.el7.x86_64
    • bash-completion-2.1-6.el7.noarch

Issue

  • In Red Hat Enterprise Linux 6, bash works as expected with bash-completion (fetched from epel) as follows.
[root@rhel6 Data]# rpm -qa --queryformat "%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH} = %{VENDOR}\n" | grep bash
bash-4.1.2-48.el6.x86_64 = Red Hat, Inc.
bash-completion-1.3-7.el6.noarch = Fedora Project
[root@rhel6 Data]# touch rpmall.txt rpmshort.txt
[root@rhel6 Data]# mkdir testdir
[root@rhel6 Data]# cp rpmall.txt rpmshort.txt $_
[root@rhel6 Data]# ls $_
rpmall.txt  rpmshort.txt
  • Whereas following doesn't work in Red Hat Enterprise Linux 7 and 8 Beta.
[root@rhel8beta Data]# rpm -qa --queryformat "%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH} = %{VENDOR}\n" | grep bash
bash-completion-2.7-5.el8.noarch = Red Hat, Inc.
bash-4.4.19-6.el8.x86_64 = Red Hat, Inc.
[root@rhel8beta Data]# mkdir testdir
[root@rhel8beta Data]# cp rpmall.txt rpmshort.txt $_
cp: target ‘_filedir’ is not a directory
  • Whereas such executions work in Red Hat Enterprise Linux 7 and 8.
[root@rhel8beta ~]# ls -l anaconda-ks.cfg 
-rw-------. 1 root root 2121 Dec  5 18:11 anaconda-ks.cfg
[root@rhel8beta ~]# ls -l $_
-rw-------. 1 root root 2121 Dec  5 18:11 anaconda-ks.cfg
  • Why does bash not keeping value of $_ for some commands?

Resolution

Behaviour of $_ and !$Special Parameters can be unpredictable in interactive mode when commands can be executed behind the scenes, e.g. when bash-completionis in place. It's not recommended to use those and rely on their value in interactive mode.

Root Cause

  • This has been reported to Engineering for further review.
  • According to bash reference manual i.e. man bash
($_, an underscore.) At shell startup, set to the absolute pathname used to invoke the shell or shell script being executed as passed in the environment or argument list. Subsequently, expands to the last argument to the previous command, after expansion. Also set to the full pathname used to invoke each command executed and placed in the environment exported to that command. When checking mail, this parameter holds the name of the mail file currently being checked. 
  • The sentence Subsequently, expands to the last argument to the previous command, after expansion leaves it unclear if $_ should be modified when a command gets executed in background. For e.g. through bash completion in this case. A discussion is being going on in upstream if this can be considered as a bug.
  • For more details, contact Red Hat Technical Support team referring this KCS solution.

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.