0

Question when run shell script with setting "-x" in centos7 or fedora28, it show different result, see below

centos7

[boomer@localhost ~]$ cat 1.sh 
#!/bin/bash

set -x

touch 你好.txt
mv 你好.txt /tmp
rm -f /tmp/你好.txt

[boomer@localhost ~]$ ls -l
总用量 4
-rw-r--r--. 1 boomer boomer 81 11月 14 16:24 1.sh
drwxr-xr-x. 2 boomer boomer  6 11月 14 16:17 公共
drwxr-xr-x. 2 boomer boomer  6 11月 14 16:17 模板
drwxr-xr-x. 2 boomer boomer  6 11月 14 16:17 视频
drwxr-xr-x. 2 boomer boomer  6 11月 14 16:17 图片
drwxr-xr-x. 2 boomer boomer  6 11月 14 16:17 文档
drwxr-xr-x. 2 boomer boomer  6 11月 14 16:17 下载
drwxr-xr-x. 2 boomer boomer  6 11月 14 16:17 音乐
drwxr-xr-x. 2 boomer boomer  6 11月 14 16:17 桌面
[boomer@localhost ~]$ echo 你好
你好
[boomer@localhost ~]$ export | grep LANG
declare -x GDM_LANG="zh_CN.UTF-8"
declare -x LANG="zh_CN.UTF-8"
[boomer@localhost ~]$ touch 你好.txt
[boomer@localhost ~]$ mv 你好.txt /tmp
[boomer@localhost ~]$ rm -f /tmp/你好.txt
[boomer@localhost ~]$ sh 1.sh 
+ touch $'\344\275\240\345\245\275.txt'
+ mv $'\344\275\240\345\245\275.txt' /tmp
+ rm -f $'/tmp/\344\275\240\345\245\275.txt'

Environment

[boomer@localhost ~]$ uname -a
Linux localhost.localdomain 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[boomer@localhost ~]$ free -m
              total        used        free      shared  buff/cache   available
Mem:            992         606          73           5         311         206
Swap:          2047           2        2045
[boomer@localhost ~]$ cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core) 
[boomer@localhost ~]$ 

fedora28

[boomer@bzha ~]$ ls
 1.sh                devo-ui-jenkinsfile   es/          git.jar                     jmeter.log     sonarqube/    vimbk/
 bin/                dive.log              fh-mvn/      gitstats/                   maven/         ssss.tar.GZ   vimgo/
'Calibre Library'/   Documents/            Fiberhome/   go/                         Pictures/      svn/          vimpython/
 config.xml          Downloads/            fz/          gogs-repositories/          python/        Templates/    更新.png
 Desktop/            dwhelper/             github/      jenkins.pipeline.template   serviceCheck   vimbash/
[boomer@bzha ~]$ cat 1.sh 
#!/bin/bash

set -x 
touch 你好.txt
mv 你好.txt /tmp
rm -f /tmp/你好.txt
[boomer@bzha ~]$ sh 1.sh 
+ touch 你好.txt
+ mv 你好.txt /tmp
+ rm -f /tmp/你好.txt
[boomer@bzha ~]$ export| grep LANG
declare -x GDM_LANG="en_US.UTF-8"
declare -x LANG="en_US.UTF-8"
[boomer@bzha ~]$ free -m
              total        used        free      shared  buff/cache   available
Mem:          11743        7775         220         277        3747        3466
Swap:          7845          67        7778
[boomer@bzha ~]$ uname -a
Linux bzha 4.18.13-200.fc28.x86_64 #1 SMP Wed Oct 10 17:29:59 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[boomer@bzha ~]$ cat /etc/fedora-release 
Fedora release 28 (Twenty Eight)
[boomer@bzha ~]$ sh 1.sh 
+ touch 你好.txt
+ mv 你好.txt /tmp
+ rm -f /tmp/你好.txt

you see, it show chinese characters which is expected in fedora28, then show octal characters in centos7, my question is how to make it show normal characters in centos7 ?

zhashuyu
  • 161
  • 2
  • 9
  • Can you check if `echo $'\344\275\240\345\245\275.txt'` is the same as `echo "你好.txt"`? Can you post the output of `locale` on both os-es? – KamilCuk Nov 14 '18 at 08:47
  • Yes >[boomer@localhost ~]$ cat /etc/centos-release >CentOS Linux release 7.4.1708 (Core) >[boomer@localhost ~]$ echo $'\344\275\240\345\245\275.txt' >你好.txt > >[boomer@bzha ~]$ cat /etc/fedora-release >Fedora release 28 (Twenty Eight) >[boomer@bzha ~]$ echo $'\344\275\240\345\245\275.txt' >你好.txt >[boomer@bzha ~]$ – zhashuyu Nov 14 '18 at 08:50
  • I try debian \ kali, it appears ok , so I just switch base os to debain! – zhashuyu Nov 16 '18 at 06:41

1 Answers1

0

They are not garbled, they are unambiguous. You will notice that the shell reformats many other strings with set -x, too.

tripleee
  • 175,061
  • 34
  • 275
  • 318