cp -R "~/photo dir" /backups #method1
cp -R ~"/photo dir" /backups #method2
cp -R ~/"photo dir" /backups #method3
"~/" 展开为用户的主目录,然后附加包含空格的目录名称。"~/" 展开为用户的主目录,然后附加包含空格的目录名称。"~/" 展开为用户的主目录,然后附加包含空格的目录名称。"$HOME/photo dir" 才会成功。$ ls -1
Beach photo1.jpg
Photo1.jpg
Photo2.jpg
Script.sh
$ cat script.sh
for i in $(ls *.jpg); do
mv $i ${i}.bak
done
( command )sh command{ command; }(( command ))reference。Subshell是程序员捕获(通常是为了处理)程序或脚本输出的一种方式。要在subshell中运行的命令需要用单括号括起来,并在前面加上美元符号:DIRCONTENTS=$(ls -l) echo ${DIRCONTENTS}
echo "1 2 3" | awk '{for (i=1; i<=NF; i++) s=s+$i};END {print s}'
reference。AWK是一种编程语言,专为处理基于文本的数据而设计,无论是文件中的数据还是数据流,或者通过shell pipes。换句话说,你可以将awk与shell scripts结合使用,或者直接在shell prompt下使用。
find / -name "finance.db" 1>results.txt 2>/dev/null
reference。将stderr(standard error)重定向到文件的语法:command 2> errors.txt。
sed -i '/^$/d' textfilesed '/^$/d' textfilecat textfile | sed '/^$/dsed -i 's/^$//' textfilereference
sed:sed是一种stream editor,用于对input stream执行基本的文本转换。
-i[SUFFIX]:此选项指定文件将在原地编辑。
‘/^$/d’:正则表达式位于//之间。^表示行的开头,$表示行的结尾。^$表示开头和结尾之间没有任何内容。
d:删除pattern space;立即开始下一循环。
Warning,上述示例在Mac terminal上由于不同的UNIX flavours可能无法工作。可以通过添加额外的flag -e或--来使其在Mac上工作(参考StackOverflow):sed -i -e '/^$/d' textfile.txt
awk -F: '/user1/{print $1 "-" $3 "-" $6}' /etc/passwd
reference。传统上,/etc/passwd文件用于记录每个注册用户的信息,这些用户可以访问系统。/etc/passwd文件是一个colon-separated文件,包含以下信息:1-Username,2-Password,3-User ID (UID),4-Group ID (GID),5-User ID Info (GECOS),6-Home directory,7-Command/shell
"set -e"会发生什么?reference。set -e选项指示bash如果任何command [1]返回非零exit status,则立即退出。你可能不希望在command-line shell中设置此选项,但在script中它非常有用。在所有广泛使用的general-purpose programming languages中,未处理的runtime error——无论是Java中抛出的exception,还是C中的segmentation fault,或者Python中的syntax error——都会立即停止程序的执行;后续行不会被执行。
mysql < file.sql > file.txt
Note:查看下面的问题以了解变体。
mysql < file.sql > out.txt
Note:查看上面的问题以了解变体。
reference。Linux和Unix的access rights flags setuid和setgid(分别是set user identity和set group identity的缩写)[1]允许用户以可执行文件owner或group的file system permissions运行可执行文件,并在directories中更改behavior。
cat {$1,textfile}cat textfile | awk [print $1]cat textfile | awk '{print $1}'awk textfile {print $1}(reverse-i-search)`':
Note:在Mac上,它会显示bck-i-search:而不是(reverse-i-search)。
var=$( expr 10 / 8 )(( var= 10 /8 ))var=$(( 10 / 8 ))var=$(echo 'scale=2; 10 / 8' | bc)reference。bc命令用于command line calculator。它类似于basic calculator,可以用来进行基本的数学计算。带有两位precision的division将传递给bc,进行计算,并分配给变量。
txt=Penguins
[[ $txt =~ [a-z]{8} ]]; echo $?
HAL>
SHELL="HAL\>"SHELL="HAL>"export PS1="HAL>"PS1="HAL\>"VAR="/var/www/html/website.com/html/"
echo "${VAR#*/html}"
/website.com/html//html/website.com/html//var/www/html/website.com/reference
根据POSIX shell specification:${parameter#[word]}。Remove Smallest Prefix Pattern。word将被expanded以生成一个pattern。parameter expansion将导致parameter,删除与pattern匹配的最小prefix部分。
例如,${VAR#?}扩展为$VAR的值,删除第一个字符。而${VAR#\*/html}扩展为包含所有字符直到并包括/html的文本,这些内容将从变量中删除,生成输出/website.com/html/。
#!/usr/bin/env bash~/usr/bin/env bash'$!/usr/bin/env bash#/usr/bin/env bashThe date is: Sun Mar 24 12:30:06 CST 2019!
echo "The date is: !"echo "The date is: date!"echo "The date is: (date)!"echo "The date is: $(date)!"A. /home/demo.sh
B. ./demo.sh
C. ~/demo.sh
D. bash /home/demo.sh
E. bash demo.sh
find . -type htmlfind . -name *.htmlfind *.htmlfind . -name \*.html -print第二个选项看起来不错,但如果当前工作目录中有任何.html文件,\*会被扩展。
cat < in.txt > out.txt
(( $a == $b ))
echo $?
$a和$b的值之间循环。$a和$b的值是否相等。$b的值大于$a,它会返回$b。$a的值大于$b,它会返回$a。; ;: :done$$#!/usr/bin/env bash
case $num in
1)
echo "one"
;;
2)
echo "two"
;;
*)
echo "a mystery"
;;
esac
touch file{1+10}.txttouch file{1-10}.txttouch file{1..10}.txttouch file(1..10).txt$$$?$!$@#!/bin/bash
fname=john
john=thomas
echo ${!fname}

A 
B 
C 
D 
以下是Q.30的文本版本:
ll
-rw-r--r-- 1 frankmolev staff 374 Jun 3 19:30 .
-rw-r--r-- 1 frankmolev staff 1666 Jun 3 19:30 ..
-rw-r--r-- 1 frankmolev staff 0 Jun 3 19:30 file1.txt
-rw-r--r-- 1 frankmolev staff 0 Jun 3 19:30 file2.txt
..
ll | sed -e 's,file,text,g'
-rw-r--r-- 1 frankmolev staff 374 Jun 3 19:30 .
-rw-r--r-- 1 frankmolev staff 1666 Jun 3 19:30 ..
-rw-r--r-- 1 frankmolev staff 0 Jun 3 19:30 file1.file
-rw-r--r-- 1 frankmolev staff 0 Jun 3 19:30 file2.file
..
-rw-r--r-- 1 frankmolev staff 374 Jun 3 19:30 .
-rw-r--r-- 1 frankmolev staff 1666 Jun 3 19:30 ..
-rw-r--r-- 1 frankmolev staff 0 Jun 3 19:30 file1.txt
-rw-r--r-- 1 frankmolev staff 0 Jun 3 19:30 file2.txt
..
-rw-r--r-- 1 frankmolev staff 374 Jun 3 19:30 .
-rw-r--r-- 1 frankmolev staff 1666 Jun 3 19:30 ..
-rw-r--r-- 1 frankmolev staff 0 Jun 3 19:30 text1.file
-rw-r--r-- 1 frankmolev staff 0 Jun 3 19:30 text2.file
..
-rw-r--r-- 1 frankmolev staff 374 Jun 3 19:30 .
-rw-r--r-- 1 frankmolev staff 1666 Jun 3 19:30 ..
-rw-r--r-- 1 frankmolev staff 0 Jun 3 19:30 text1.txt
-rw-r--r-- 1 frankmolev staff 0 Jun 3 19:30 text2.txt
..
A. find . -name '%text%'
B. find . -name '*text*'
'%text%'完全匹配的所有文件,而B会搜索名称包含substring “text”的所有文件。"*text*"完全匹配的所有文件。#!/bin/bash
echo "1=$1"
echo "2=$2"
echo "$(( 4 * 5 + 3 / 2 ))"
forwhilecaseuntilfind . -name sales.dbfind . -name sales.dbfind . -db sales.dbfind . -file sales.db注意:两个正确答案的选项看起来相同,这可能是原始问题的打字错误。
VAR="This old man came rolling"
echo "${VAR//man/rolling}"
if [ -x "$file" ] 测试什么?exportsudomodulesecure|\;&sort | uniq -usort -usort --uniquesort -uniqcat file.txt | while IFS= read -r line; do
echo $line
done
exec bashsource bashnew bashopen bash#!/usr/bin/env bash
echo $1
cat < test.txt > test.txt
if [ -a "$myvar" ]; then echo "Test was true"; fiif [ -n "$myvar" ]; then echo "Test was true"; fiif [ "$myvar" ]; then echo "Test was true"; fi注意:一些来源说B是正确的,因为它测试变量是否有值。但是题目问的是”测试变量存在”,所以C可能更准确。
fordo-whilerepeatloop#!/bin/bash#!/bin/sh#!/bin/basher#!/usr/bin/env bashcat <<EOF
-------------------------
This is line 1.
This is line 2.
This is line 3.
-------------------------
EOF
This is line 1.
This is line 2.
This is line 3.
-------------------------This is line 1.This is line 2.This is line 3.-------------------------
-------------------------
This is line 1.
This is line 2.
This is line 3.
-------------------------
-------------------------
This is line 1.
This is line 2.
This is line 3.
-------------------------
untildo-untilforwhile$$$!$?$@$?$-$@$0\t\s\n\dexec 3>word.txtexec 4<word.txtexec 3>word.txtexec 3>&1$$$?$!$@for i in $(ls); do
echo $i
done
2>3>1>&>#!/bin/bash
echo $1
for file in *.csv; do awk -F',' '{print $3}' $file | grep profit; donefor file in *.csv; do grep profit $file | awk -F',' '{print $3}'; donefor file in *.csv; do grep -F',' profit $file | awk '{print $3}'; donefor file in *.csv; do awk '{print $3}' $file | grep -F',' profit; doneVAR='Hello World!'
echo ${VAR:6:5}
echo 'Hello, $(whoami)!'
tar -ssh user@192.158.1.1 /bin/newfiletar cvzf - /wwwdata | ssh root@192.168.1.201 "dd of=/backup/wwwdata.tar.gz"scp -r directory user@192.168.1.1:/tmpls -lah 分配给shortcut command lh,应该使用什么命令?alias lh='ls -lah'link lh='ls -lah'alias 'ls -lah'=lhlh | ls -lah$ ls -l
file10.txt
file1.txt
fileabc.txt
filea.txt
fileb.txt
filec.txt
$ ls -l file[^abc]*.txt
file1.txt
file10.txt
file10.txt
file1.txt
fileabc.txt
filea.txt
fileb.txt
filec.txt
fileabc.txt filea.txt fileb.txt filec.txt
filea.txt
fileb.txt
filec.txt
reference
此处的caret(^)表示否定brackets内的匹配项。
cat <<EOF
------------------------
This is line 1.
This is line 2.
This is line 3.
------------------------
EOF
This is line 1.
This is line 2.
This is line 3.
------------------------This is line 1.This is line 2.This is line 3.------------------------
------------------------
This is line 1.
This is line 2.
This is line 3.
------------------------
------------------------
This is line 1.
This is line 2.
This is line 3.
------------------------
#!/bin/bash
echo 123446789 > out.txt
exec 3<> out.txt
read -n 4 <&3
echo -n 5 >&3
exec 3>&-
#!/bin/bash
shopt -s extglob
VAR=' This is... a string of characters '
VAR=${VAR##+([[:space:]])}; VAR=${VAR%%+([[:space:]])};
echo "$VAR"
<pre> This is... a string of characters</pre><pre> This is...a string of characters</pre><pre>This is... a string of characters</pre><pre>This is...a string of characters</pre>Reference:
echo $((4/3))
cat > notes -
sed -E -n '/^(.)(.)\3\2\1$/p'sed -E -n '/^(.)(.)(.).\2\1$/p'sed -E -n '/^(.)(.)(.)\2\1$/p'sed -E -n '/^(.)(.)(.)(.)\3\2\1$/p'[[$A==$B]]
[[$A -eq $B]]
VAR="united states"
echo "${VAR^}"
#!/bin/bash
#condition 1
if [ $foo = "bar" ]; then echo "foo is bar"
fi
#condition 2
if [[ $foo = "bar" ]]; then echo "foo is bar"
fi
Explanation:脚本如写的那样会输出line 3: [: =: unary operator expected。定义variable并assign value foo="bar",两个conditions都会成功。
$#$@0$!$ID$@$#$$cat -n animals | sort -r | head -n 5
1 Ant
2 Bear
3 Cat
4 Dog
5 Elephant
9 Ibex
B Hippo
7 Giraffe
6 Fox
5 Elephant
4 Dog
3 Cat
2 Bear
1 Ant10 Jaguar
Jaguar
Ibex
Hippo
Giraffe
Fox
9 Ibex
8 Hippo
7 Giraffe
6 Fox
5 Elephant
$HOMEmy_var1var!以下哪个one-liner Bash command可以完成此task?
find . -name "*.txt" -exec wc -l {} \; | awk '{total += $1} END {print total}'grep -r ".*\.txt$" | wc -lfind . -type f -name "*.txt" | xargs wc -l | tail -n 1find . -name "*.txt" -exec cat {} \; | wc -l> 会覆盖target file的内容,而 >> 会将内容追加到target file的末尾。> 用于重定向input,而 >> 用于重定向output。> 用于standard output,而 >> 用于standard error。> 是unary operator,而 >> 是binary operator。