cp -R "~/photo dir" /backups #method1
cp -R ~"/photo dir" /backups #method2
cp -R ~/"photo dir" /backups #method3
"$HOME/photo dir"avrà successo."~/" alla home directory dell’utente e quindi aggiungere il nome della directory tra virgolette che include uno spazio."~/" alla home directory dell’utente e quindi aggiungere il nome della directory tra virgolette che include uno spazio."~/" alla home directory dell’utente e quindi aggiungere il nome della directory tra virgolette che include uno spazio.$ 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 ))echo "1 2 3" | awk '{for (i=1; i<=NF; i++) s=s+$i};END {print s}'
find / -name "finance.db" 1>results.txt 2>/dev/null
sed -i '/^$/d' textfilesed '/^$/d' textfilecat textfile | sed '/^$/dsed -i 's/^$//' textfileawk -F: '/user1/{print $1 "-" $3 "-" $6}' /etc/passwd
"set -e" in uno script Bash?mysql < file.sql > file.txt
Nota: controlla la domanda qui sotto per una variante.
mysql < file.sql > out.txt
Nota: controlla la domanda sopra per una variante.
cat {$1,textfile}cat textfile | awk [print $1]cat textfile | awk '{print $1}'awk textfile {print $1}(reverse-i-search)`':
var=$( expr 10 / 8 )(( var= 10 /8 ))var=$(( 10 / 8 ))var=$(echo 'scale=2; 10 / 8' | 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/#!/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 -printIl secondo sembra buono, ma espanderà il * se c’è qualche file .html nella directory di lavoro.
cat < in.txt > out.txt
(( $a == $b ))
echo $?
$a e $b.$a e $b sono uguali.$b se è più grande di $a.$a se è più grande di $b.; ;: :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 
Ecco una versione basata su testo di 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 68 Jun 3 19:30 .
-rw-r--r-- 1 frankmolev staff 1666 Jun 3 19:30 ..
-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 text.txt
..
#!/bin/bash
read -p "Enter your pet type." PET
if [ $PET = dog ] ;then
echo "You have a dog"
fi
history --sharedhistory --combinedshopt -s histappend$@ considera ogni argomento citato come un’entità separata. $* considera l’intera stringa di argomenti come un’unica entità.$* considera ogni argomento citato come un’entità separata. $@ considera l’intera stringa di argomenti come un’unica entità.$* viene utilizzato per contare gli argomenti passati a uno script, $@ fornisce tutti gli argomenti in un’unica stringa.$* è il carattere jolly che include tutti gli argomenti con suddivisione delle parole, $@ contiene gli stessi dati ma in una matrice.if [ -f file.txt ]; then
echo "file.txt exists"
fi
/usr/bin/test/usr/bin/[the built-in [ command/usr/bin/[[#!/bin/bash
Linux=('Debian' 'Redhat' 'Ubuntu' 'Android' 'Fedora' 'Suse')
x=3
Linux=(${Linux[@]:0:$x} ${Linux[@]:$(($x + 1))})
echo "${Linux[@]}"
/etc/bash.conf~/.profile/etc/bashprofile~/profile$ ls -l
total 0
-rwx------+ 1 user1 user1 0 Oct 27 10:54 data.txt
+ alla fine della stringa di autorizzazione di 10 cifre indica che è presente un elenco di controllo di accesso. Questo potrebbe eventualmente dare all’utente2 autorizzazioni non visibili da ls -l.ls -l.+ alla fine della stringa di autorizzazione a 10 cifre indica che è presente un set di attributi esteso. Ciò potrebbe concedere all’utente2 le autorizzazioni per leggere, scrivere ed eseguire dati.txt.#!/bin/bash
declare -A ARRAY=([user1]=bob [user2]=ted [user3]=sally)
KEYS=(${!ARRAY[@]})
for (( i=0; $i < ${#ARRAY[@]}; i+=1 ));do
echo ${KEYS[$i]} - ${ARRAY[${KEYS[$i]}]}
done
ls Hello[[.vertical-line.]]World
Hello.vertical-line.WorldHello[[.vertical-line.]]WorldHello|Worldls nonexistentfile | grep "No such file" > out.txt
#!/bin/bash
read -p "Enter text " var
if [[ "$var" =~ "^[0-9]+$" ]];then
echo "Is numeric"
else
echo "Is not numeric"
fi
^[0-9]]+$ Solo questo si rivelerà vero e “È numerico” verrà stampato sullo schermo a causa di una sintassi errata. Incapsulando l’espressione regolare tra virgolette doppie, ogni corrispondenza avrà esito negativo tranne la stringa di testo ^[0-9]+$La regex non deve essere citata per funzionare correttamente.
| [ ] storia | trova cp |
| [x] storia | grep cp |
bash for i in $(ls); do ... donebash for $(ls); do ... donebash for i in $ls; do ... donebash for $ls; do ... done| [x] |
#!/usr/bin/env bash
greeting="Hello"
echo $greeting, everybody!
(( num -gt 5 ))[[$num -lt 5]](( num > 5 ))num > 5$ ls -l
apple
banana
bananapple
banapple
pineapple
strawberry
$ shopt -s extglob
$ ls -l @(ba*(na)|a+(p)le)
apple
banana
apple
banana
bananapple
banapple
pineapple
strawberry
apple
banana
bananappple
banapple
pineapple
apple
banana
bananapple
banapple
pineapple
ls -l
-rwx------+ 1 user1 u1 0 Oct 1 10:00 data.txt
cd -
cat > notes -
VAR="This old man came rolling"
echo "\${VAR//man/rolling}"
Shall we play a game? yes\no
echo "Shall we play a game? yes/\no"echo "Shall we play a game\? yes\\no"echo "Shall we play a game? yes\\no"echo "Shall we play a game? yes\no"archive.tar
image1.gif
image1.jpg
image2.gif
image2.jpg
textfile1.txt
textfile2.txt
----------
`shopt -s extglob
rm !(*gif|*jpg)`
archive.tar
image1.gif
image1.jpg
image2.gif
image2.jpg
textfile1.txt
textfile2.txt
archive.tar
textfile1.txt
textfile2.txt
Tutti questi file verranno eliminati
image1.gif
image1.jpg
image2.gif
image2.jpg
#!/bin/bash
var="8"
if [ $var > 5 ]; then
echo "$var is greater than 5"
fi

w
Una costante è una variabile che è una roccia che non è variabile
var="A constant is a variable that is a variable that isn't variable"
echo "$var" | sed _____
echo 'Hello, $(whoami)!'
| [x] tar cvzf - /wwwdata | ssh root@192.168.1.201 “dd of=/backup/wwwdata.tar.gz” |
ls -lah al comando di scelta rapida lh, quale comando dovresti usare?| [ ] lh | 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
Il caret (^) qui nega le corrispondenze all’interno della parentesi. Riferimento
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>&-
Vedere Reindirizzamento I/O
Vedi anche: Qual è la differenza tra “echo” e “echo -n”?
#!/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>Referenze: