87

I have two users in Fedora:

  1. Wani
  2. root (quite obvious!)

My contents of .bashrc of user Wani are:

# .bashrc
echo "Hello"
# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# User specific aliases and functions

Now after logging into root, I type the following commands:

[root@Dell Wani]# touch try.txt
[root@Dell Wani]# service sshd start
[root@Dell Wani]# scp try.txt Wani@localhost:~/
Wani@localhost's password: 
Hello
[root@Dell Wani]# 

Now I log into Wani, and type:

[Wani@Dell ~]$ cat try.txt
cat: try.txt: No such file or directory
[Wani@Dell ~]$ 

Now I again log into root and type the same command with -v:

[root@Dell Wani]# scp -v morph.log Wani@localhost:
Executing: program /usr/bin/ssh host localhost, user Wani, command scp -v -t -- .
OpenSSH_5.6p1, OpenSSL 1.0.0j-fips 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to localhost [127.0.0.1] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/id_rsa type -1
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.6
debug1: match: OpenSSH_5.6 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.6
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host 'localhost' is known and matches the RSA host key.
debug1: Found key in /root/.ssh/known_hosts:2
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-     with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure.  Minor code may provide more information
Credentials cache file '/tmp/krb5cc_0' not found

debug1: Unspecified GSS failure.  Minor code may provide more information
Credentials cache file '/tmp/krb5cc_0' not found

debug1: Unspecified GSS failure.  Minor code may provide more information


debug1: Unspecified GSS failure.  Minor code may provide more information


debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/id_rsa
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Next authentication method: password
Wani@localhost's password: 
debug1: Authentication succeeded (password).
Authenticated to localhost ([127.0.0.1]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env XMODIFIERS = @im=none
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: scp -v -t -- .
Hello
[root@Dell Wani]# debug1: client_input_channel_req: channel 0 rtype exit-status      reply      0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 1664, received 1976 bytes, in 0.1 seconds
Bytes per second: sent 22961.5, received 27266.8
debug1: Exit status 0

(And after I press Enter)

[root@Dell Wani]# 

Can anyone please shed some light as to what exactly happened here? Why did the file not get copied to Wani from root?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Nehal J Wani
  • 16,071
  • 3
  • 64
  • 89
  • What about `cp`? Does `cp try.txt ~Wani/` work? – nneonneo Sep 15 '12 at 19:20
  • It is indeed possible that interactive commands or spurious output from your `.bashrc` could ruin some noninteractive scenarios. Try making the code conditional on `case $- in *i*` or some such. – tripleee Sep 15 '12 at 19:22
  • @nneonneo: That works. Actually, even If I have the two users on different computers, the file doesn't get copied! – Nehal J Wani Sep 15 '12 at 21:02
  • 1
    All the posts I have seen describe WHY this problem exists in various bashrc files, etc. However, when SCP/STFP to a machine you DON'T OWN and CANNOT change the startup scripts, what is the solution then??? – ChuckB Jun 27 '18 at 23:08
  • I ended up redirecting all echos to stderr. Works, but... – Andreas Sep 21 '19 at 11:20

10 Answers10

84

Using echo in a .bashrc will break scp, as scp expects to see its protocol data over the stdin/stdout channels. See https://bugzilla.redhat.com/show_bug.cgi?id=20527 for more discussion on this issue.

There's a few workarounds available:

  • Condition on the 'interactive' flag (e.g. case $- in *i* as suggested by tripleee)
  • Use the tty utility to detect an interactive shell (e.g. if tty > /dev/null or if [ -t 0 ])
  • Check the value of $SSH_TTY

I suppose you should use whichever one works for you. I don't know what the best (most portable/most reliable) option is, unfortunately.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • 6
    Just to clarify the "case" command would be: `case $- in *i*) echo This is safe since the shell is interactive; esac` – mlathe Feb 26 '13 at 17:59
  • `shopt` is also an alternative – vesperto Mar 23 '18 at 18:43
  • 1
    Since my host is always accessed over SSH, SSH_TTY is always set. Using the `-t 0` condition worked well for me. I use the following function all over my bashrc now. ```safe_echo() { if [[ -t 0 ]];then echo -e "$*" fi } ``` – Sandip Bhattacharya Jan 18 '19 at 02:25
  • `if [[ "$SSH_TTY" == */dev/pts/* ]] ; then` inside if put your content which prints to stdout , then scp will work fine. – BeatingBytes May 25 '19 at 22:39
  • @BeatingBytes & @Sandip Bhattacharya, `[[` isn't portable and I also don't think it's posix. Try this in the `dash` shell: `if [[ 0 = 0 ]]; then echo x; else echo y; fi`. You get, `dash: 3: [[: not found`. – Elliptical view Aug 06 '20 at 19:34
  • `$SSH_TTY` check will not work when you first ssh to some host then do scp, such as: `$ ssh myhost` `$ scp myfile anotherhost:~` since $SSH_TTY got set on the first ssh, and never been reset in the following command(s). – Robin Hsu Jul 07 '21 at 08:20
  • You just saved me a lot of frustration thank you. – O.rka Jun 03 '22 at 21:56
20

To add to nneonneo's options, you can also condition with the interactive flag with

if [[ $- =~ "i" ]]

which I think is possibly the clearest way in bash.

Drew Ogle
  • 479
  • 4
  • 4
  • `[[` isn't portable and I also don't think it's posix. Try this in the `dash` shell: `if [[ 0 = 0 ]]; then echo x; else echo y; fi`. You get, `dash: 3: [[: not found`. – Elliptical view Aug 06 '20 at 19:36
18

This works for me,
In .bashrc add first line as:

if [ -z "$PS1" ]; then
    return
fi

https://superuser.com/questions/690735/can-i-tell-if-im-in-an-scp-session-in-my-bashrc

Community
  • 1
  • 1
Manoj Ashok
  • 189
  • 1
  • 3
13

The default Ubuntu .bashrc contains the following snippet which already takes care of the problem:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
    *) return;;
esac
phil294
  • 10,038
  • 8
  • 65
  • 98
  • 4
    shorter: `[[ $- != *i* ]] && return` – phil294 Dec 14 '18 at 00:31
  • 3
    The form provided in your answer is POSIX compatible. What you offer in your comment is not POSIX because of the use of `[[` feature. Not so problematic for a bashrc file ;) but you might want to be able to use it in another context/shell. – Totor Nov 21 '19 at 14:36
4

In .bashrc, use STDERR as output instead:

echo "# Important Notice" >&2

Update: do not use it! We had an issue recently that a (closed source) tool failed due to an echo to STDERR in .bashrc. The tool (using rcp) expected no output at all, neither on STDOUT nor STDERR. And it stuck when it got the echo. Lesson learned: make separate accounts for humans and for machines (scripts), or just stop tattling via .bashrc.

user151841
  • 17,377
  • 29
  • 109
  • 171
bal
  • 49
  • 2
2

The most portable way of testing for an interactive shell seems to be:

test -t 0
if [ $? -eq 0 ]
then
    # interactive
    ;
else
    # non-interactive
    ;
fi
Jambock
  • 21
  • 1
1

nneonneo's solution worked for me as well. But since my default shell is TCSH, I had to slightly edit the fix as follows (in .tcshrc):

if ( $?SSH_TTY ) then
    exec /bin/bash
endif

Just thought I would share for everyone's benefit.

0

If you're on Red Hat Enterprise Linux (RHEL) or variant, drop a script that does the echo, or whatever you want, into /etc/profile.d/

Everett Toews
  • 10,337
  • 10
  • 44
  • 45
0

If you want echo statements as well, the from @Blauhirn's answer, you can keep placing your echo statement/s after the case condition.

case $- in
    *i*) ;;
    *) return;;
esac

echo "Your Greeting/Warning Message/s here!"
-1
if [ 0 -eq $(shopt -q login_shell; echo $?) ]; then
  echo "do something?"
fi

Source

vesperto
  • 804
  • 1
  • 6
  • 26
  • It would be nice to provide some context for the OP. – allanberry Mar 23 '18 at 21:51
  • Context and relevant information can already be extracted from other answers and the question itself. This is just another way of doing what is already understood, with a low signal-to-noise ratio. – vesperto Mar 24 '18 at 20:59