������������� ������� ��������� ��������� ���������� (man-��)
bash ()
��������
bash — ��������� ������������� GNU Bourne-Again SHell
���������
-
bash [�����] [����]
��������� �����
��������� ������������� bash ������ ������ ��������
����������������� ������������ �����������.
Copyright (C) 1989-1999 by the Free Software Foundation, Inc.
��������
bash — ��� sh-����������� ������������� ���������� �����,
����������� �������, ����������� �� ������������ �������� ������
��� �� �����. ��������� ������������� bash ����� ��������
�������� �������� ��������� ��������������� Korn � C (ksh � csh).
��������� ������������� bash ���������� ��� ���������������
������������ ���������� �������������� � ���������������� �������
IEEE POSIX (IEEE
POSIX Shell and Tools specification)
(IEEE Working Group 1003.2).
�����
������ ������������� ����� ���������� ��������������, ��������������
� �������� ���������� ������� set, bash
�������������� ��� ������ ��������� �����:
- -c ������
-
���� ������� ����� -c, ������� �������� �� ������. ���� ����� ������
���� ���������, �� �������� ������������� ����������� ����������,
������� � $0.
- -r
-
���� ������� ����� -r, ��������� ������������� ���������� ������������
(��. «������������ ��������� �������������» ����).
- -i
-
���� ������� ����� -i, ��������� ������������� �������� ���
�������������.
- -s
-
���� ������� ����� -s ��� ���� ����� ��������� ����� ����������
�� ��������, ������� �������� �� ������������ �������� ������.
��� ����� ��������� ������������� ����������� ���������
��� ������ �������������� ���������� ��������������.
- -D
-
������ ���� ����� � ������� �������� � ��������� $
�������� � ����������� �������� �����. ��� ������,
������� ���������� ��������� �� ��������������� ����, ����
������� ������ ���������� �� C ��� POSIX. ��� ���� ������
�������������� ����� -n; ������� ������� ����������� �� �����.
- —
-
���� ������� (—) ���������� ����� ����� � ��������� �� ����������
���������. ����� ��������� ����� — ��������������� ��� ����� ������
� ���������. �������� — ������������ —.
��������� ������������� bash ����� �������������� ���������
��������������� �����. ��� �����, ����� ��� ���� ����������, ������
����������� � ��������� ������ ����� ��������������� �������.
- —dump-po-strings
-
���������� ����� -D, �� ��������� �������� � ������� ����� GNU
gettext po (portable object — ����������� ������).
- —dump-strings
-
���������� ����� -D.
- —help
-
������ � ����������� �������� ����� ��������� �� ������������� �
������� ��������� ������.
- —login
-
���������� bash �������� ���, ��� ���� �� �� ��� ������ � ��������
���������� ���������� ��������������
(��. «�����» ����).
- —noediting
-
�� ���������� ���������� GNU readline ��� ������ ��������� ����� �
������������� ������.
- —noprofile
-
�� ������ �� ������������� ���� ���������� �������
/etc/profile, �� ����� �� ������������ ������ �������������
������������ ~/.bash_profile, ~/.bash_login ��� ~/.profile.
�� ���������, bash ������ ��� ����� ��� ������ � ��������
���������� ���������� �������������� (��. «�����» ����).
- —norc
-
�� ������ � �� ��������� ������������ ���� �������������
~/.bashrc, ���� ��������� ������������� �������� ������������. ��� �����
�� ��������� ��������, ���� ������������� ������ ��� sh.
- —posix
-
�������� �� ����������� ��������� bash � ��� �������, ����� ���
���������� �� ��������������� ���������� POSIX 1003.2.
- —rcfile ����
-
��������� ������� �� ���������� ����� ������ ������������ �������������
����� ������������� ~/.bashrc, ���� ��������� ������������� ��������
������������ (��. «�����» ����).
- —restricted
-
��������� ������������� ���������� ������������
(��. «������������ ��������� �������������» ����).
- —verbose
-
���������� ����� -v.
- —rpm-requires
-
������� ������ ������, ����������� ��� ���������� �������� ��������. ��� ������������� ������� ‘-n’ � ����� �� �� �����������, ��� � �������� ������ �� ����� ����������; �������� �������, []-�������� � ��������� �� �������� �������, ����� ������� ��������� ����������� ����� ��������� ���������.
- —version
-
������ ���������� � ������ ������� ���������� bash � ����������� �������� �����
� ������� ��������� ������.
���������
���� ����� ��������� ����� �������� ��������� � �� ������� �����
-c ��� -s,
������ �������� ��������� ������ �����, �����������
������� ��������������. ���� bash ������ ����� �������, �������� $0
��������������� ������ ����� �����, � ���������� ����������� ����������
���������� ������ ���������. ��������� ������������� bash ������ �
��������� ������� �� ����� �����, � ����� ��������� ������. ��������
������ bash �������� ������ ������ ��������� �������, ����������� � ��������.
���� �� ���� ������� �� ���������, ������ ������ — 0.
�����
��������� ��������� ������������� (login shell) — ��� �������������,
������ ������ �������� ��������� �������� �������� ������� (—) ���
������� ������� � ������ —login.
������������� �������� ��������� �������������, ����������� ������� �
�������� ������ �������� ���������� � ���������� (��� ������������
� ������� ������� isatty(3)), ��� ����������
� ������ -i. ����������
����� PS1 ��������������� � ����� ($-) �������� i, ������ ���� bash ��������
������������� ���������������, ��� ��������� ��������� ��� � �����
���������� ������� ��� � �������� ���������� ��������������.
��������� ������ ���������, ��� bash ��������� ���� ����� ����������
�������. ���� ����� �� ���� ������ ����������, �� �� ����� ����
��������, bash ������ ��������� �� ������. ������� ������ (~)
� ������ ������ ���������� ���, ��� ������� � ����������
«������ ������» �������
«������«.
��� ������ bash ��� �������������� ���������� ���������� ��������������,
��� ��� ���������������� �������������� � ������
—login, �� �������
������ � ��������� ������� �� ����� /etc/profile, ���� ���� ���� ����������.
����� ��������� ����� �����, �� ��������������� ���� ����� ~/.bash_profile,
~/.bash_login � ~/.profile, ������ � ��������� ������� �� ������� �� ��
���, ������� ���������� � �������� �� ������. ����� —noprofile �����
�������������� ��� ������� ���������� ��������������, ����� ��������
��� ��������.
��� ���������� ������ � �������� ���������� ���������� ��������������
bash ������ � ��������� ������� � ����� ~/.bash_logout, ���� �� ����������.
��� ������� �������������� ���������� ��������������, �� �����������
���������, bash ������ � ��������� ������� �� ����� ~/.bashrc, ����
�� ����������. ��� �������� ����� �������� � �������
����� —norc.
����� —rcfile ���� ���������� bash
������ � ��������� ������� �� ���������� ����� ������ ~/.bashrc.
��� ��������������� ������� bash, ��������, ��� ���������� ��������,
��������� ������������� ���� � ����� ���������� BASH_ENV,
�������� �� �������� � ���������� ��� � �������� ����� �����, �������
���������� ��������� � ���������. bash ����� ���� ���, ���
���� �� ���� ��������� ��������� �������:
-
if [ -n «$BASH_ENV» ]; then . «$BASH_ENV»; fi
�� ��� ������ ����� ����� �� ������������ �������� ���������� PATH.
���� bash ������ � ������ sh, �� ��������, �������� ��� ��������,
����������� ��������� ��� ������� ������ ������ �������������� sh,
������������ ��� ���� ��������� POSIX. ��� ������ � ��������
�������������� ���������� ���������� �������������� ���
����������������, �� � ������ —login, �� ������� �������� ������ �
��������� ������� ��������������� �� ������ /etc/profile � ~/.profile.
��� ������ ����� �������� ����� ������������
����� —noprofile. ��� ������
� �������� �������������� ���������� �������������� � ������
sh, bash ���� ���������� ����� ENV, �������� �� ��������, ���� ��� ������,
� ���������� ��� �������� � �������� ����� �����, ������� ����������
��������� � ���������. ��������� �������������, ��������� ��� sh, ��
�������� ������ � ��������� ������� � ������ ������ ���������� �������,
����� —rcfile �� ���������. ��������������� �������� �������������,
��������� � ������ sh, �� �������� ������ � ��������� ������� ����� ����������
�������. ��� ������ �� ����� sh, ��������� ������������� bash ������ � �����
posix ����� ��������� ������ ���������� �������.
��� ������� bash � ������ posix, ��������, � ������� ����� ��������� ������
—posix, �� ������� ��������� POSIX ��� ������ � ������� ���������� �������.
� ���� ������ ������������� ��������� ��������������
����� �������� ���������� ����� ENV, ������ � ��������� ������� ��
���������������� �����. ������� ������ ����� ����������
������� �� ��������.
��������� ������������� bash �������� ����������, �� ������� �� ��
������� ���������� ���������� ��������������, ������, rshd. ����
�����������, ��� bash ������� ������� rshd, �� ������ � ���������
������� �� ����� ~/.bashrc, ���� ���� ���� ���������� � ��������
�� ������. ��������� ������������� bash ��� �� ������, ���� �������
��� sh. ��� ������ ����� �������� ����� ������������ �����
—norc, � � ������� �����
—rcfile ����� ��������� ������ ������ ����,
�� ������ ����� rshd �� ������ ��� ����� ���������� �������������� � ��
��������� �� �������.
���� ��������� ������������� ������� � ����������� ���������������
������������ (������), �� ����������� � �������� ���������������
������������ (������), � �� ������� ����� -p, �����
���������� ������� �� ��������, ������� ���������� �������������� ��
����������� �� �����, ���������� SHELLOPTS, ���� ��� ������ � �����,
������������, � ����������� ������������� ������������ ���������������
������ ���������. ���� ��� ������ ������ ����� -p,
����� ���������� �������
���� �� ��������, �� ����������� ������������� ������������ �� ������������.
�����������
� ���������� ����� ��������� ������������ ��������� �������.
- ������
-
������ ������� ��� ������ ���������.
- �����
-
������������������ ��������, ��������������� ��������� ���������������
��� ������ �����. ����� ���������� �������� (token). - ���
-
�����, ��������� ������ �� ����������������� �������� � �������� �������������,
� ������������ � ����� ��� ������� �������������. �����
���������� ���������������. - ����������
-
������, ����������� �����, ���� �� �� ������������. ���� �� ���������
��������:
-
| & ; ( ) < > ������ ��������� - ����������� ��������
-
�������, ����������� ������� ����������. ��� ���� �� ��������� ��������:
-
|| & && ; ;; ( ) | <������� ������>
����������������� �����
������������������ �������� �����, ������� ����������� �������� ���
���������� ��������������. ��������� ����� ������������ ���
�����������������, ���� �� ������������� � �������� ���� ������ ������
������� ������� (��. «��������� ������» ����),
���� ������� ������ ������� case ���
for:
! case do done elif else
esac fi for function if in
select then until while { }
time [[ ]]
��������� ������
������� �������
������� ������� — ��� ������������� ����������� ����������
������������������ �������������� ������������ �������� ����������,
����� ������� ���� �����, ����������� ���������, �
��������������� �������. ������ ����� ������ �������, �������
���� ���������. ���������� ����� ���������� ��� ��������� ���������
�������.
������������ ��������� ������� ������� �������� ��
������ ������, ��� 128+n, ���� ������� ��������� �������� n.
���������
�������� — ��� ������������������ ����� ��� ����� ������,
����������� �������� |. �������� ����� ��������� ������:
-
[time [-p]] [ ! ] ������� [ | �������2 … ]
����������� �������� ����� ������� ����������� �� ����������� �������
������� �������2. ��� ����� ����������� ������, ��� ����� ���������������,
���������� �������� (��. «���������������» ����).
���� ����� ���������� ������� ����������������� ����� !,
������ ������ ������ ��������� �������� ���������� ����������
������� ������ ��������� �������. � ��������� ������, ������
������ ��������� ��������� �� �������� ������ ��������� �������.
������, ��� ���������� ��������, ��������� ������������� ���� ����������
���� ������ � ���������.
���� ����� ���������� ���� ����������������� ����� time,
����� ���������� ��������� �������� �����, ���������������� �
��������� �����, ����������� �� ��� ����������.
����� -p ��������
������ ������ �� ���������� ���������� POSIX. ����������
TIMEFORMAT ����� �������� ������ ������� ������ ���������� � �������;
��. �������� ���������� TIMEFORMAT � �������
«���������� ���������� ��������������» ����.
������ ������� � ��������� ����������� ��� ��������� �������
(�.�. � ����������� ��������������).
������
������ — ��� ������������������ ������ ��� ����� ����������,
����������� ����� �� ���������� ;, &, &&
��� ||, � �� ����������� ������������� ����� �� ����������
;, & ��� <������� ������>.
�� ���� ���������� ������ && � || ����� ������ ���������, ������
�������, ��� ; � &, ���� ������� ������ ���������.
���� ������� ����������� ����������� ���������� &, �������������
��������� ������� � ������� ������ � ����������� ��������������.
��������� ������������� �� ���� ���������� �������, � ������
������ � ���� ������ — 0. �������, ����������� ; �����������
���������������; ��������� ������������� ���� ���������� ���������� ������
�� ������. ������ �������� ������ � ���� ������ ��������� ��
�������� �������� ��������� ����������� �������.
����������� ��������� && � || ����������, ��������������, �-������
� ���-������. �-������ ����� ���
-
������� && �������2
�������2 ����������� ������ � ���� ������ ������� ������� ������
������ ����.
���-������ ����� ���
-
������� || �������2
�������2 ����������� ������ � ���� ������ ������� ������� ���������
������ ������. �- � ���-������ ���������� ������ ��������� �������,
����������� � ������.
��������� �������
���������� �������� ��������� �������:
- (������)
-
������ ����������� � ����������� ��������� ��������������.
������������ ���������� � ���������� �������, �������� ��
����� ���������� ��������������, �� ��������� ����� ����������
�������. �������� �������� �������� ������ ������ ������.
- { ������; }
-
������ ������ ����������� � ����� �������� ���������� ��������������.
������ ������ ����������� ��������� ������ ��� ������ � �������.
��� ������� �������� �������� �����������. �������� ��������
�������� ������ ������ ������.
- ((���������))
-
��������� ����������� � ������������ � ���������, ���������� ����
� �������
«���������� �������������� ���������«. ���� ���������
����� ��������� ��������, ������ �������� ����� 0; � ���������
������ — 1. ��� ����������� ������������ ����������� let «���������».
- [[ ��������� ]]
-
���������� ������ 0 ��� 1 � ����������� �� �������� ����������
��������� ���������. ��������� ������� �� �����������, ���������
���� � ������� «�������� ���������«.
������� ���� � �����������
���� ������ � ������ ����� [[ � ]] �� �����������;
����������� ������ ������, ����������� �������� ���������� � ����������,
�������������� �����������, ����������� ����������� ������ �
����������� ���������, � ����� �������� �������.
��� ������������� ���������� == � != ������ ������ ���������
��������� �������� � �������������� �� ��������, ��������� ���� �
���������� «������������� � ��������«. ������������ ��������
0 ���� ������, ��������������, �������������� � �� ��������������
� ��������, � 1 � ��������� ������. ����� ����� ������� ����� ����
����� � �������, ����� ������������� � ��� ��� ���������.
��������� ����� ������������� � ������� ��������� ����������,
������������� � ������� �������� ����������:
- ( ��������� )
-
���������� �������� ���������. ������� ������ ����� ������������
��� ��������� �������� ���������� ����������.
- ! ���������
-
�������, ���� ��������� �����.
- ���������1 && ���������2
-
�������, ���� ������� ��� ���������, ���������1 �
���������2.
- ���������1 || ���������2
-
�������, ���� ������� ���������1 ��� ���������2.
��������� && � || �� ��������� ���������2, ����
�������� ���������1 ���������� ��� ����������� �������������
�������� ����� ��������� ���������.
- for ��� [ in ������_���� ] ; do ������ ; done
-
����������� ����������� � ������_���� ����� in, � ���������� ����
���������� ������ ���������. ���������� � ���������������
������ ��������������� �������� �������� ������� �������� �����
������, � ������ ��� ����������� ������. ���� ����� in �� �������,
������� for ��������� ������ ��� ������� �������������� ������������
��������� (��. ������ «���������» ����). ������� ��������
�������� ������ ������ ��������� ����������� �������. ���� �
���������� ����������� ��������� ������_���� ����� in ����������
������ ������, ������� �� ����������� � ������������ ������ 0.
- select ��� [ in ������_���� ] ; do ������ ; done
-
����������� ����������� � ������_���� ����� in, � ���������� ����
���������� ������ ���������. ����� ���������� ����� �����������
���� �������� � ����������� ����� ������, ������ ����� ������
��������� �������� ��� ���������� �����. ���� ����� in �� �������,
�������� ������������� ����������� ���������
(��. ������ «���������» ����). ����� �������� ����������� PS3
� �������� ������ �� ������������ �������� ������. ���� ��� ������
�������� �����, ��������������� ������ �� �������� ����, �� ����������
��� �������� ��� ��������. ���� ������ ������, ����� � �����������
�������� �����. ���� �������� ������ ����� ����� (EOF), �������
�����������. ��� ����� ������ ������� �������� ���������� ���
�������� ������ ��������. ����������� ������ ����������� �
���������� REPLY. ������ ����������� ����� ������� ������, ���� ��
����� ��������� ������� break ��� return. �������� ������
������� select �������� ������ ������ ��������� ������� � ������,
��� 0, ���� �� ���� ������� �� ���� ���������.
- case ����� in [ ( ������� [ | ������� ] … ) ������ ;; ] … esac
-
������� case ������� ����������� �������� ����� � ��������
��� ����������� ���������� � ������ ��������, ��������� �� ��
������� �������������, ��� � ��� ���� ������ (��. ��������� «�����������
���� ������«). ���� ������ ���������������� �������,����������� ��������������� ������. ����� ���������� �������
������������� ���������� ������������� �� ����������� � �������
�����������. ������ ������ ����� 0, ���� �� ���� ������� �� ������������.
� ��������� ������, �� ����� ������� ������ ��������� �����������
������� �� ���������������� ������.
- if ������; then ������; [ elif ������; then ������; ] … [ else ������; ] fi
-
����������� ������ ����� if. ���� ��� ������ ������ — 0,
����������� ������ ����� then. � ��������� ������, �����������
��������������� ������ � ��������� ����������� elif, � ���� ���
������ ������ — 0, ����������� ��������������� ������ ����� then
� ������� �����������. ���� ��� ����������� elif ���������
� �� ���� �� ������� �� ������ ������ 0, ����������� ������ ����� else,
���� ��� ����������� ������. �������� ������ ��������
������ ������ ��������� ����������� ������� ��� 0, ���� �� ���� �� �������
�� ��������� ��������.
- while ������; do ������; done
-
until ������; do ������; done
������� while ���������� ��������� ������ ����� do, ����
��������� ������� ������ ���������� ������ ������ 0.
������� until ��������� ������� while, �� ������� ���������� —
��������������; ������ ����� do �����������, ���� ��������� �������
������ ���������� ��������� ������ ������. �������� ������ ������
while � until �������� ������ ������ ��������� �����������
������� ������ ��� 0, ���� �� ���� ������� �� ���������.
- [ function ] ��� () { ������; }
-
��� ������� ���������� ������� � ��������� ������. ����
������� �������� ������ ������ ����� ��������� ��������{ � }. ���� ������ ����������� ������ ���, �����
��� ������� ����������� ��� ��� ������� �������. �������� ������ �������
�������� ������ ������ ��������� �������, ����������� � ����
�������. (��. ������ «�������» ����.)
�����������
� ��������������� ��������� �������������� ��� � ������������� �
���������� ������ interactive_comments ���������� �������
shopt (��. ������
«���������� ������� ��������������» ����), �����,
������������ �������� #, ������ ������������� ����� ����� � �����������
�������� � ������. ������������� ��������� ������������� � �� ����������
������ interactive_comments �� ��������� ������������� ������������.
����� interactive_comments �� ��������� ���������� � ������������� ���������
���������������.
����������
���������� (quoting) ������������ ��� ������ ������������ �������� ���
���������� �������������� ������������ �������� ��� ����.
���������� ����� ������������ ��� ������ ������������� ��������� �����������
��������, ��� �������������� ������������� ����������������� ����, � �����
��� �������������� ����������� ����������.
������ �� ������������, ������������� ���� � ������� «�����������«,
����� ����������� �������� ��� ���������� �������������� � ������
�������������, ���� ������������ ���������. ������� ��� ��������� ����������:
������ ���������� (escape character), ��������� ������� � ������� �������.
����������������� �������� ����� ����� �������� �������� ����������. ��
������� ������������ ��������� �� ��� ������ (�� ����������� �������� ������)
���������. ���� ������� ���� ��������
<������� ������> � ���� ��������
����� �� �������������, ��� ���� �������������� ��� ������� ����������� ������
(�.�. ��� ��������� �� �������� ������ �, �� ����, ������������).
��� ������� � ��������� �������� ������������ ���������. ������ ���������
������� (��������) �� ������ ����������� ����� ���������� ���������, ���� ����
�� ������������ �������� �����.
������� � ������� �������� ������������ ���������, �� ����������� ��������
$, ` �
�������� ����� ����� ����������� �������� ������ ���� ����� ��� ���� ����
�� ��������� ��������: $, `, «,
��� <������� ������>. ������� ������� �����
��������� � ������� ��������, ������������ �������� �����.
����������� ��������� * � @ ����� ����������� �������� ��� �������� � �������
�������� (��. ������ «���������» ����).
����� ���� $’������’ �������������� ������ �������y. ����� ����������
�������, � ������� ������������ �������� ����� ������� ���������� � ������������
�� ���������� ANSI C. ����������� ������������������, ������������ � ��������
�����, ������������ ��������� �������:
a
������� (������)
b
�����
-
e
����������� ������
-
f
������ �������� (form feed)
-
n
������� ������ (new line)
-
r
carriage return (������� �������)
-
t
���������
-
v
������������ ���������
-
\
�������� �����
-
‘
��������� �������
-
nnn
������, ASCII-��� �������� ��������� � ������������ ��������� nnn (�� ����� �� ���� ����)
-
xHH
������, ASCII-��� �������� ��������� � ����������������� ��������� nnn
(�� ����� �� ���� ����)
-
cx
������� control-x
��������������� ��������� ���������� � ��������� �������, ��� ���� ��
������� ������� ������ �� ����.
������ � ������� ��������, ����� ������� ���� ������ ������� ($), �����
������������� � ������������ � ������� �������. ���� ������� ������ —
C ��� POSIX, ������ ������� ������������. ���� ������ �������������
� ��������, ��������� ���������� � ������� �������.
���������
�������� — ��� ��������, �������� ��������. ��� ����� ����
���, ����� ��� ���� �� ����������� ��������, ������������� ���� �
������� «����������� ���������«. � ���������
��������������, ���������� — ��� ��������, ������������ ������.
�������� ���������������, ���� ��� ��������� ��������. ������ ������ ��������
���������� ���������. ����� ����, ��� ���������� �����������, ��� ����� ����
������� ������ � ������� ���������� ������� unset
(��. ������ «���������� ������� ��������������» ����).
�������� ���������� ����� ���� ��������� � ������� ��������� ����
-
���=[��������]
���� �������� �� ������, ���������� ������������� ������ ������. �� ����
��������� ����������� ������ ������, ����������� �������� ���������� �
����������, ��������� �����, ����������� ����������� ���������� ������,
�������������� ���������� � �������� �������� ���������� (��.
������ «���������» ����). ���� ��� ����������
���������� ������� ������������� (integer)
(��. declare � �������
«���������� ������� ��������������»
����), �� �������� ������������� �����������, ���� ���� ��������������
���������� $((…)) �� ������� ���� (��. ���������
«�������������� ����������» ����). ��������� �� ����� �� �����������, �� �����������
�������� «$@», ��� ��������� ���� � ����������
«����������� ���������«.
����������� ���� ������ �� �����������.
����������� ���������
����������� �������� — ��� ��������, ������������ ����� ��� �����������
�������, ����� ����� 0. ����������� ���������� �������� ������������� ��
������ ����������, ���������� ��� ������ ���������� ��������������, � �����
���� ������������� � ������� ���������� ������� set. �����������
���������� ������ ����������� �������� � ������� ���������� ������������.
����������� ��������� �������� ���������� ��� ���������� ������� ����������
�������������� (��. ������ «�������» ����).
����� ����������� �������� ������� �� ���������� ����, ��� ���� �����
� ������ (��. ������ «���������» ����).
����������� ���������
��������� ������������� ������������ ��� ���������� ����������� �������.
����� ������ ��������� �� �������� ���� ���������� — ������������ �� ��
���������.
*
���������� ������������ �����������, ������� � �������.
����� ����� ������ ����������� � ������� ��������, ��������� �����
����� ������, � ������� �������� ���������� ��������� ������ ��������
�������� ����������� ���������� IFS. �� ����, «$*» ������������
«$1c$2c…», ��� c — ������ ������ �������� ���������� IFS. ���� ����������
IFS �� �����������, �������� ���������� ����������� ���������. ���� ����������
IFS ����� ������ ��������, �������� ���������� ������ ���������������, ���
������������.
- @
-
���������� ������������ �����������, ������� � �������.
����� ����� ������ ����������� � ������� ��������, ������ ��������
���������� ��������� ������. �� ����, «$@» ������������
«$1» «$2» … ����� ����������� ���������� ���, «$@»
� $@ ���������� ������ ������� (�.�. ��� ������ ���������).
- #
-
���������� ���������� ��������� ���������� ����������� ����������.
- ?
-
���������� �������� ���������� ������������ � ������������ ������ ���������.
- —
-
���������� ������� ������� �����, �������� ��� ������, � �������
���������� ������� set ��� ������������� ����� ���������
��������������� (���, ��������, ����� -i).
- $
-
���������� ��������������� �������� ���������� ��������������.
� ����������� ��������� ��������������, ���������� � ������� (), ����������
��������������� �������� ��������� ��������������, � �� ������������.
- !
-
���������� ��������������� ���������� ��������, ����������� � �������
(�����������) ������.
- 0
-
���������� ������ ���������� �������������� ��� ��������. ��� ��������
��������������� ��� ������������� ���������� ��������������. ����
bash ������ � ��������� ������, $0 ����� ���������� ������ �����
����� �����. ���� bash ������� � ������ -c,
$0 �������� ��������
������� ��������� ����� ������, ������� ���� ���������, ���� �� �����.
� ��������� ������, ���� ����������� �������� �������� �������� �����
�����, ��������������� ��� ������ bash, ������� �������� �������
����������.
- _
-
��� ������� ���������� �������������� ��������������� ������ �������
����� ����� (absolute file name) ���������� �������������� ��� ��������������������, ����������� � ������ ����������. � �����������,
���������� ��������� ���������� ���������� �������, � ������ ���� �����.
����� ��������������� ������ ������� ����� ����� ������ ����������� �������
������� � ���������� � �����, �������������� ��� ���� �������. ��� ��������
����� ���� �������� �������� ��� ������������ ����� �����.
���������� ���������� ��������������
��������� ������������� ������������� ��������� ����������:
PPID
������������� ������������� �������� ��� ���������� ��������������.
��� ���������� �������� ������ ��� ������.
- PWD
-
������� ������� �������, ������������� � ������� ������� cd.
- OLDPWD
-
���������� ������� �������, ������� ��� ���������� � ������� �������
cd.
- REPLY
-
��������������� ������ ��������, ������������ ���������� ��������
read, ��� ������ �� ��� ����������.
- UID
-
������������� �������� ������������ — ��������������� ��� ������� ����������
��������������. ��� ���������� �������� ������ ��� ������.
- EUID
-
����������� ������������� �������� ������������ — ���������������
��� ������� ���������� ��������������. ��� ���������� ��������
������ ��� ������.
- GROUPS
-
����������-������, ���������� ������ �����, � ������� ������ �������
������������. ��� ���������� �������� ������ ��� ������.
- BASH
-
������ ��� �����, �������������� ��� ������ ������� ���������� bash.
- BASH_VERSION
-
������, ����������� ������ ������� ���������� bash.
- BASH_VERSINFO
-
��������� ������ ��� ������ ������, �������� �������� �������� ����������
� ������ ��� ������� ���������� bash. �������� ������� ����� ���������
��������:
- BASH_VERSINFO[0]
-
������� ����� ������ (�����).
- BASH_VERSINFO[1]
-
�������������� ����� ������ (������).
- BASH_VERSINFO[2]
-
������� �����������.
- BASH_VERSINFO[3]
-
������ ������.
- BASH_VERSINFO[4]
-
������ ������ (��������, beta1).
- BASH_VERSINFO[5]
-
�������� ���������� MACHTYPE.
- SHLVL
-
������������� �� 1 ��� ������ ������� ������������ �������������� bash.
- RANDOM
-
��� ������ ��������� � ����� ��������� ������������ ��������� �����
�� 0 �� 32767. ���������� ������������������ ��������� �����
����� ���� ������������������� ����� ������������ �������� ����������
RANDOM. ���� ���������� RANDOM ��������� � �������
unset, ���
������ ���� ����������� ��������, ���� ���� � ���������� �������������������.
- SECONDS
-
��� ������ ��������� � ����� ��������� ������������ ���������� ������,
��������� � ������� ������ ������� ���������� ��������������. ���� ����������
SECONDS ������������� ��������, ��� ����������� ��������� ����� ��������
���������� ������, ��������� � ������� ������������, ���� �����������
��������. ���� ���������� SECONDS ���������
� ������� unset, ���
������ ���� ����������� ��������, ���� ���� � ���������� �������������������.
- LINENO
-
��� ������ ��������� � ����� ��������� ��������� �������������
����������� ���������� �����, �������������� ���������� ����� �������
������ (������� � 1) � �������� ��� �������. ��� ������ �� ��
�������� ��� �������, �� ������������� ����������� ������������ ��������.
���� ���������� LINENO ���������
� ������� unset, ���
������ ���� ����������� ��������, ���� ���� � ���������� �������������������.
- HISTCMD
-
������������ ����� �������, ��� ������ ������� ������� � ������ �����������
������. ���� ���������� HISTCMD ���������
� ������� unset, ���
������ ���� ����������� ��������, ���� ���� � ���������� �������������������.
- DIRSTACK
-
����������-������ (��. ������ «�������» �����),
� ������� �������� ������� ����������
����� ���������. �������� ��������� � ����� � ��� �� �������, � �������
�������� ���������� �������� dirs.
����� ������������ ��������� �����
������� ����� �������� ��������, ��� ����������� � �����, �� ���
���������� � �������� ��������� ���� ������������ ���������� �������
pushd � popd.
������������ �������� ���� ���������� �� ������ ������� �������.
���� ���������� DIRSTACK ���������
� ������� unset, ���
������ ���� ����������� ��������, ���� ���� � ���������� �������������������.
- PIPESTATUS
-
����������-������ (��. ������ «�������» �����), ���������� ������ ��������
�������� ������ ��������� � ��������� ����������� � ������������ ������
��������� (������� ����� �������� � ����� �� ����� �������).
- OPTARG
-
�������� ���������� ���������-�����, ������������� ���������� ���������
getopts (��. ������
«���������� ������� ��������������» ����).
- OPTIND
-
������ ���������� ���������, ������� ������ ���� ��������� ����������
�������� getopts (��. ������
«���������� ������� ��������������» ����).
- HOSTNAME
-
������������� ��������������� ������ ����� �������� �����.
- HOSTTYPE
-
������������� ��������������� ������ ������, ��������� ����������� ���
������, �� ������� ����������� bash. ����������� �������� ������� �� �������.
- OSTYPE
-
������������� ��������������� ������ ������, ����������� ������������ �������,
�� ������� ����������� ��������� ������������� bash. ����������� ��������
������� �� �������.
- MACHTYPE
-
������������� ��������������� ������ ������, ��������� ����������� ��� �������,
�� ������� ����������� bash, � ����������� ��� GNU �������
���������-��������-�������. ����������� �������� ������� �� �������.
- SHELLOPTS
-
������ ������������� ����� ���������� �������������� ����� ���������.
������ ����� � ������ — ���������� �������� ��� ����� -o ����������
������� set (��. ������
«���������� ������� ��������������» ����).
�����, ������������� � ������ SHELLOPTS, ��������� � �����������
��� ������������� �������� set -o.
���� ��� ���������� �������������� � �����
��� ������� ���������� �������������� bash, ��� ���������� � ������
����� ����� ����������� �� ������ ����� ������ ���������� �������.
��� ��������� �������� ������ ��� ������.
��������� ���������� ������������ ��������� ���������������. � ���������
������� bash ����������� ���������� ����������� ��������; ��� ������ �������
����.
IFS
���������� ����������� ����� (Internal Field Separator), ������������
��� ��������� ���� ����� ���� ����������� � ��� ��������� �� ����� �����,
����������� � ������� ���������� ������� read.
����������� �������� — «<������><���������><������� ������>».
- PATH
-
���� ������ ������. ��� ������ ��������� ����� ���������, � �������
��������� ������������� ���� �������
(��. ������ «���������� ������»
����). ����������� ���� ������ ������� �� ������� � �������� ���������������,
��������������� bash. �������� ��������:
«/usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin:.».
- HOME
-
��������� ������� �������� ������������; ����������� �������� ��� ����������
������� cd. �������� ���� ���������� ����� ������������
��� ������ ������.
- CDPATH
-
���� ������ ��� ������� cd. ��� ������ ��������� �����
���������, � ������� ��������� ������������� ���� ������� ��������, ���������
�� ����� � ������� cd. ������ ����������
�������� — «.:~:/usr».
- BASH_ENV
-
���� ���� �������� ���������� ��� ���������� ��������� ���������������
bash ��������, ��� �������� ���������������� ��� ��� �����, ����������� �������
������������� ��������������, ���������� ~/.bashrc.
� �������� BASH_ENV ����� �������������� ����������� ����������� ��������
����������, ����������� ����������� ���������� ������ � ��������������
����������. ���������� PATH �� ������������ ��� ������ ��������������� �����
�����.
-
���� ���� �������� ���������� ������ ����� �����
� �� ����������� ���������� MAILPATH, bash ����������� ������������
� ��������� ����������� ����� � ��������� ����.
- MAILCHECK
-
������ ������� �������� (� ��������) ����������� ����� ���������
��������������� bash. ����������� �������� — 60 ������.
����� �������� ����� ��������� ������� ����� �����, ���������
������������� ������ ��� ����� ������� ���������� �����������.
���� ��� ���������� �� �����������, �������� ����� ���������
��������������� �����������.
- MAILPATH
-
������ ���� ������ ����� ���������, � ������� ����� ��������� �������
����� �������� ���������. ����� �������� ���������, ������� ������
���������� ��� ����������� ����� � ���������� ����, ������� ��������� ��
����� ����� �������������� ������ (?). � ������ �����
��������� ���������� $_ ���������� ������ �������� ����� �����. ��������:
MAILPATH=’/usr/spool/mail/bfox?»You have mail»:~/shell-mail?»$_ has mail!»‘
��������� ������������� bash ������������ ����������� ��������
��� ���� ����������, �� ������������ ��� ���� ��������������� ������ �����
������������ ������� �� ������� (��������, /usr/spool/mail/$USER).
- PS1
-
�������� ����� ��������� �������������� (��.
������ «�����������» ����) �
������������ ��� ��������� ������ ���������� �����������. �����������
�������� — «s-v$ «.
- PS2
-
�������� ����� ��������� ��������������, ��� � PS1, �
������������ ��� ��������� ������ ���������� �����������.
����������� �������� — «> «.
- PS3
-
�������� ����� ��������� ������������ � ��������
����������� ��� ������� select
(��. ������ «��������� ������» ����).
- PS4
-
�������� ����� ��������� ��������������, ��� � PS1, �
�������� ����� ������ ��������, ������������ ���������������
bash � ���� ����������� ����������. ������ ������ PS4
����������� ����������� ���������� ���, ��������� �������
�����������. ����������� �������� — «+».
- TIMEFORMAT
-
�������� ����� ��������� ������������ � �������� ������, �������� ������
������ ���������� � ������� ���������� ��� ����������, ����� ��������
������ ����������������� ����� time. ������ % �������� �����������
������������������, ���������� ��������� ������� ��� ������ �����������.
����������� ������������������ ����� ��������� �������� (� ����������
������� ������������ �������������� �����):
%%
������ %.
- %[p][l]R
-
����� ����������, � ��������.
- %[p][l]U
-
���������� ������ ������ ���������� � ���������������� ������.
- %[p][l]S
-
���������� ������ ������ ���������� � ��������� ������.
- %P
-
������� ������������� ����������, ����������� ��� (%U + %S) / %R.
�������������� �������� p — ��� �����, �������� ��������,
���������� ������ (����� �������) ����� �������. �������� 0
�������� ������ ����� �������. ������������ �������� — ��� ����� ����� �������;
�������� p ������ 3 ���������� �� 3. ���� p �� ������, ������������ �������� 3.
�������������� �������� l ������ ������� ������, ������� ������, � ����
MMmSS.FFs. �������� �������� p ����������, ���������� ���� ������� ��� ���.
���� ��� ���������� �� �����������, bash ��������� ���, ��� ���� ��
��� ����� �������� $’nrealt%3lRnusert%3lUnsys%3lS’. ���� �����������
������ ��������, ���������� � ������� ���������� �� ��������.
��� ������ ������ �� ������� ����������� ������� ������.
- HISTSIZE
-
���������� ������, ������� ���������� ���������� � ������ ������� (��.
������ «������� ������» ����). ����������� �������� — 500.
- HISTFILE
-
��� �����, � ������� ����������� ������� ������ (��. ������
«������� ������» ����).
����������� �������� — ~/.bash_history.
���� ��� ���������� �� �����������, ������� ������ �� ����������� ���
���������� ������ �������������� ���������� ��������������.
- HISTFILESIZE
-
������������ ���������� �����, ������������ � ����� �������.
���� ���� ���������� ��������� ��������, ���� �������, ��� �������������,
���������, � ����� ��������� �� ������, ��� ��������� ���������� �����.
����������� �������� — 500. ���� ������� ����� ��������� �� ����� ������� �����
������ ��� ������ �� �������������� ���������� ��������������.
- OPTERR
-
���� ����� �������� 1, bash ���������� ��������� �� �������, ����������
���������� �������� getopts (��. ������
«���������� ������� ��������������»
����). ���������� OPTERR ���������������� ��������� 1 ��� ������ ������
���������� �������������� ��� ���������� ��������.
- LANG
-
������������ ��� ����������� ������ ��� ���� ���������, �� �������� ���� �
������� ����������, ��� ������� ���������� � LC_.
- LC_ALL
-
��� ���������� �������������� �������� ���������� LANG � ���� ���������
���������� LC_, �������� ��������� ������.
- LC_COLLATE
-
��� ���������� ������ ������� ����������, ������������ ��� ����������
���������� �� ������� ���� ������, � ���������� ������������� ����������,
������� ��������������� � ������� ��������� �������� ��� ����������� ����
������ � ������������� � ��������.
- LC_CTYPE
-
��� ���������� ���������� ������������� �������� � �������������� ��������
� ������������� ������ ��� ����������� ���� ������ � ������������� � ��������.
- LC_MESSAGES
-
��� ���������� ���������� ������, ������������ ��� ��������� ����� � �������
��������, ����� �������� ������ ������ $.
- PROMPT_COMMAND
-
���� �������� ������, ��� ����������� ��� ������� ����� ������� �������
���������� �����������.
- IGNOREEOF
-
��������� ��������� ���������� �������������� ��� ��������� �������
����� ����� (EOF) ��� ������������� ������� � ������. ������� ���������� �����������, �� �������� ���������������� ��� ����������
������ ������ �������� EOF, � ������� ������ ���������� ������, ����� �
���������� ������ ���������� �������������� bash �����������. ����
���������� ����������, �� �� ����� ��������� �������� ��� �� ����� �������� ������,
�� ��������� ������������ �������� 10. ���� ��� ���������� �� ����������,
EOF �������� ����� ������� ������ ��� ���������� ��������������.
- TMOUT
-
���� ����������� �������� ������ ����, ��� ���������������� ��� ����������
������ �������� ����� ����� ������ ���������� �����������. ���������
������������� bash ��������� ������ �� ��������� ����� �������, ���� ��
������� �������.
- FCEDIT
-
������ ����������� �������� ��� ���������� ������� fc.
- FIGNORE
-
������ ��������� ����� ���������, ������� ���������� ������������ ���
���������� ���� ������ (��. ������
«���������� READLINE» ����). ��� �����,
������� �������� ��������� � ����� �� ��������� � FIGNORE
��������, ����������� �� ������ ��������������� ���� ������.
������ �������� — «.o:~».
- GLOBIGNORE
-
������ �������� ����� ���������, �������� ����� ���� ������, �������
���� ������������ ��� ����������� ����. ���� ��� �����, ��������������� �������,
����� ������������� ������ �� �������� � GLOBIGNORE, ��� ���������
�� ������ ���������������.
- INPUTRC
-
��� ����� ���������� ������� readline, ����������������� �����������
��������� � ����� ~/.inputrc (��. ������
«���������� READLINE» ����).
- HISTCONTROL
-
���� ����� �������� ignorespace, ������, ������������ ��������
�������, �� �������� � ������ ����������� ������. ���� ����� ��������
ignoredups, ������, ����������� � ��������� ����������� ��������, � ������
����������� ������ �� ��������. �������� ignoreboth �������� ��������
����� �������������� �����. ���� ���������� ��� ��� ��� ����� �����-�� ������
��������, ����� ������������� ����, ��� ������, ����������� ��������������
������������, ����������� � ������ �������, � ������ �������� ����������
HISTIGNORE. �������� ���� ���������� ���������������� ����������
HISTIGNORE. ������ � ��������� ������ ������������� ��������� ������� ��
�����������, � ����������� � ������ ������� ���������� �� ��������
HISTCONTROL.
- HISTIGNORE
-
������ �������� ����� ���������, ������������ ��� �������� ������� � ����������
��������� ����� � ������ �������. ������ ������ ������������� � ������
������ � ������ �������� ��� ������ (‘*‘ ������ �� �����������). ������
������ ������������ �� ������� ����� ��������, ����������
���������� HISTCONTROL. ����� ������� �������� ������������� � ��������
� ��������� ��������������, ������ ‘&‘ �������������� � ���������� �������
� ������ �������. ������ ‘&‘ ����� ������������ � ������� �������� �����. �����
�������������� �������� ����� ���������. ������ � ����������� ������ ���������
������������� ������� �� ����������� � ����������� � ������ ������� ���������� ��
�������� ���������� HISTIGNORE.
- histchars
-
��� ��� ��� �������, ����������� ������������ � ���������� �� �������
��� ����������� ������ (��. ������
«����������� ������ �� ������ �����������»
����). ������ ������ — ��� ������ ����������� �� ������, �������
�������� ������ ������ �� ������� �������. ������ ��� ������ ‘!‘.
������ ������ — ��� ������ ������� �����������, ������������ ��� ��������
���������� ���������� ��������� �������, � ������������ ��������� ������ ������
������, �������� � ���� �������. �� ��������� ������������ ������ ‘^‘.
�������������� ������ ������ — ��� ������, �����������, ���� ��������
������ �������� �����, ��� ������� ������ — ��� �����������. ������ � ����
�������� ������������ ������ ‘#‘. ������ ����������� ��� ������ �������
�������� � ������ �� ����������� �� ������� ��� ���������� ���� � ���������
������. �� �� ����������� �������� � ��������� �������������� ������������
���������� ����� ������ ��� �����������.
- HOSTFILE
-
�������� ��� ����� ���� �� �������, ��� � ���� /etc/hosts, �������
����� �������� ��� ������������� ����������� ����� �����. ���� ����
����� ������ ������������; ��� ��������� ������� ����������� �����
����� bash ������� ���������� ������ ����� � ��� ������������ ���� ������.
- auto_resume
-
��� ���������� ��������� ��������������� ���������� �������������� �
������������� � ���������� ���������� ���������. ���� ��� �����������,
������� ������� �� ������ ����� � ��� ��������������� ���������������
��� ��������� ���������� ������������� ������������ ������������� �������.
��������������� �� �����������; ���� � ��������� ������ ����������
����� ������ �������, ���������� ��������� �� ������� ������������ �������.
��� �������������� �������, � ���� ���������, — ��� ��������� ������,
���������������� ��� ��� �������. ���� ��� ���������� ����� ��������
exact, ��������� ������ ������ ����� ��������� � ������ �������������� �������;
���� �� ������ �������� substring, ��������� ������ ������ ����
���������� ����� �������������� �������. ��� ���������� �� ������������
�������������� ������� %?
(��. ������ «���������� ���������» ����).
���� ����������� ����� ������
��������, ��������� ������ ������ ���� ��������� ����� ��������������
�������; ��� ���������� �� ������������ �������������� ������� %.
�������
��������� ������������� bash ������������ ���������� �������.
����� ���������� ����� �������������� ��� ������; ����������
��������� declare ��������� �������� ������ ����.
��� ����������� �� ����� ������� ��� ���������� ������������� ��������. �������
������������� ������ �������, ������� � ����.
������ ��������� ������������� ��� ������������ ����� ���������� ��������
� ������� ����������� ���[������]=��������. ������ �������������� ���
�������������� ���������, ������� ������ ����� ��������������� ��������.
��� ������ ���������� ������� ������������ ������� declare -a ���
(��. ������ «���������� ������� ��������������»
����). ����������� declare -a ���[������] ����� ��������������;
������ ������ ������������. ��� ���������� ���� ������ ����� ������ ��������
� ������� ���������� ������ declare �
readonly. ������� ����������� �� ���� ��������� �������.
������������ �������� ����������� � ������� ����������� ����
���=(��������1 … ��������n), ��� ������ �������� ����� ���
[������]=������. ����������� �������� ������ ������. ���� ��������������
���������� ������ � ������ �������, ������������ ����������� �������� �
��������������� ��������; � ��������� ������, � �������� ������� ��������
������������ ������ ���������� ��������, �������� ���� ��������� ��������,
���� ����. ���������� ���������� � ����. ��� �� ����������� ������������ �����
������������ � �� ���������� ������� declare.
�������� ��������� ��������� ������� ������������� � ������� ��������������
���� ����������� ���[������]=��������.
�� ����� ������� ������� ����� ��������� � ������� �����������
${���[������]}. �������� ������ ����������, ����� �������� ���������� ���
����������� ���� ������. ���� � �������� ������� ������ ������ @ ��� *,
����� ���������� ����� ���������� ������� � ��������� ������. ��� �������
���������������� ��-������� ������ ���� ����� ������� � ������� ��������.
���� ����� ������� � ������� ��������, ����������� ${���[*]} ����������
����� ������, �������� �������� �������� ��� �������� ������� ��������
�������, ����������� ������ �������� �� �������� ���������� ����� IFS, �
����������� ${���[@]} �������� ������ ������� ������� ��� ��������� ������.
����� ������ ������, ${���[@]} ���������� ������ �������. ��� ���������� ������
����������� ���������� * � @ (��.
��������� «����������� ���������» ����).
����������� ${#���[������]} ���������� ������ �������� ${���[������]}.
���� � �������� ������� ������ ������ * ��� @, ������������� ���������� ���������
�������. ������ �� ����������-������ ��� ������� ������������ ������ ��
������� � �������� ����.
��� ����������� �������� ������������ ���������� �������
unset.
������� unset ���[������] ���������� ������� ������� � ��������� ��������.
������� unset name, ��� name — ��� �������, ��� unset ���[������],
��� ������ — * ��� @, ������� ���� ������.
���������� ������� declare,
local � readonly
������������ ����� -a ��� �������� �������.
���������� ������� read ������������ ����� -a
��� ������������ ������� ������ ����, ����������� �� �������������
�������� ������. ���������� ������� set �
declare ������ �������� ��������
���, ��� �� ����� �������� ������������ � �������������.
������
������ ����������� � ��������� ������ ����� ��������� �� �� �����.
����������� ���� ����� �����: ������ ��������� � �������� �������,
������ ������, ����������� �������� ���������� � ����������, �����������
����������� ���������� ������, ����������� �������� ��������������
���������, ��������� ���� � ����������� ���� ������.
������ ����������� � ��������� �������: ������ ��������� � �������� �������,
������ ������, ����������� �������� ����������, ����������, ��������������
��������� � ����������� ���������� ������ (����� �������), ��������� ����
� ����������� ���� ������.
� ��������, ��� ��� ����� ��������������, ����������� �������������� ������ —
����������� ���������.
������ ������ ��������� � �������� �������, ��������� ����
� ����������� ���� ������ ����� �������� ���������� ���� � ���������
������; ��� ������ ������� ���� ����� ������ ���������� ������. ������������
���������� — ����������� «$@» � «${���[@]}», ��� ����������� ����
(��. ������ «���������«).
������ ��������� � �������� �������
������ ��������� � �������� ������� — ��� �������� ��������� ������������
�����. �� ���������� ����������� ���� ������, �� ������������ �����
�� ����������� ������ ������������. ������� � �������� ������� ����� ���
��������������� ��������, �� ������� ���� ����� ����� ����� ������� � ��������
�������, ����� ���� — �������������� �������. ������� ����������� � ������
������ ������, ������������ � �������� �������, � ����� � ������ ����������
��� (����� �������) ������ ����������� �������.
��������� � �������� ������� ����� ���� ����������. ���������� ������ ������ ��
�����������; ������� ����� ������� �����������. ��������, �����������
a{d,c,b}e ���������� �� ‘ade ace abe‘.
������ ��������� � �������� ������� ����������� ����� ������ ������� ��������, �
� ���������� ����������� ��� �������, ������� ����������� �������� ��� ������
�����. ��� ������ — ������ ������������. ��������� ������������� bash �����
�� ��������� �������� ����������� ��� ����� � �������� �������.
���������� ��������� � �������� ������� ������ ��������� �����������������
����������� � ����������� �������� ������ �, �� ������� ����, ���� �����������������
�������. ����� ������������ ��������� � �������� ������� �������� ����������.
������ { ��� , ����� ������������� �������� ����� ��� �������������� ���
������������� �� ����� ������ ��������� � �������� �������.
��� ����������� ������ ������������ ��� ����������� ������ ������ ����� �
����� ���������, ����� ������� ��� � �������������� ���� �������:
-
mkdir /usr/local/src/bash/{old,new,dist,bugs}���
-
chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}
������ ��������� � �������� ������� ������ ��������� ���������������
� ������������� �������� sh. ��������� ������������� sh �� �������������
����������� � ����������� �������� ������ � ����� ����������� �������
� ������ ��������� ��. ��������� ������������� bash ������� �������� ������
�� ����� ��� ������. ��������, �����, ��������� � sh ��� file{1,2},
�������� ��� ���������. ��� �� ����� ���������� ����� ����
file1 file2 ����� ������ ��������� � �������� ������� � bash.
���� ��������� ������ ������������� � sh, ��������� ������������� bash
���� ��������� � ������ +B ��� ��������� � ������� �����
+B ������� set (��. ������
«���������� ������� ��������������» ����).
������ ������
���� ����� ���������� ����������������� �������� ������ (~),
��� ������� �� ������ ����������������� ����� ����� (��� ��� �������,
���� ����������������� ����� ����� � ����� ���) ��������� ������-���������.
���� �� ���� �� �������� ������-�������� �� ������������, ��� ������� �����
������ ��������������� ��� ��������� ��������������� ���. ���� ���
��������������� ��� ������������ ����� ������ ������, ������ ����������
��������� ���������� HOME. ���� ���������� HOME �� �����������, �������������
��������� ������� ������������, ������������ ������� ���������� ��������������.
���� �� ������-������� �� ������, ������������� ��������� �������
���������������� ������������.
���� ������-������� ����� ��� ~+, ������ ���� ������������� ��������
���������� PWD. ���� ������-������� ����� ��� ~-, ������ ���� �������������
�������� ���������� OLDPWD, ���� ��� ���������� �����������.
���� ����� �������� ����� ������ � ������-�������� ������� �����
N, � ��������������� ���������� + ��� —, ������-������� ����������
��������������� ��������� �� ����� ���������, ������� ��� �� �����
���������� �������� dirs ��� ������ � ������-��������� �
�������� ���������. ���� ������� ����� ������ �������� ����� ��� ���������� �����
+ ��� —, �������������� +.
���� ���������������� ���������������� ����� �� ���������� ���
������ ������� ��������� �� �������, �������� ����� �������� ��� ���������.
������������ �������� ���������� ����������� �� ������� �����������������
������-��������� ����� ����� : ��� =. � ���� ������� ����� �����������
������ ������. ��������������, ����� ������������ ����� ������ � �������� �
������������� ���������� PATH, MAILPATH � CDPATH, � ���������
������������� �������� ����������� ������.
����������� �������� ����������
������ $ ���������� ����������� �������� ����������, �����������
���������� ���������� ������ ��� ����������� �������� �������������� ���������.
��� ��������� ����� ����� � �������� ������, ��� ��������� �������� ���
�� ������ ��������������� �� ��� ��������, ������� �� �������� ������ �����.
��� ������������� �������� ������, ����������� ��������� ������ ������
}, �� ��������������� �������� �����, �� �������� � ������ � ��������, ����������
�������������� ��������� ��� ������������� �������, ��� � ����������� ��������
������� ���������.
- ${��������}
-
������������� �������� ���������� ���������. �������� ������ ����������,
���� �������� �������� ����������� � ����� ������� ������� �� ����������
���� ��� ����� ����� ��������� ���� ������, �� �������� � ��� ���.
���� ������ ������ ��������� — ��������������� ����, ���������� ���������
�����������. ��������� ������������� bash ���������� �������� ����������, ���
������� �������� ��������� ������ ���������, � �������� ����� ����������,
�������� ������� ���������� ����������, � �� ���������� ��������. ���
���������� ��������� �������.
� ������ �� ��������� ���� ������� � ���������� ����� ����������� ������ ������,
����������� �������� ����������, ����������� ����������� ���������� ������ �
���������� �������������� ���������. ���� �� ����������� ����������� ��������,
bash ���������, �� �������� �� �������� �� ������������� ��� �� ����� �� ��
������� ��������; ���� �� ������� ���������, ����������� ������, ���������� ��
��������.
- ${��������:-�����}
-
������������ ����������� ��������. ���� �������� �� ���������� ��� ����� ������
��������, ������������� �������� ���������� �����. ����� ������������� ��������
���������.
- ${��������:=�����}
-
��������� ����������� ��������. ���� �������� �� ���������� ��� ����� ������
��������, �������� �������� �������� ���������� �����. ����� ������������� �������� ���������. ����������� ����� ������� �������� ����������� �
����������� ���������� ������.
- ${��������:?�����}
-
�������� ������ ���� �������� �� ���������� ��� ����� ������ ��������.
���� �������� ����� ������ �������� ��� �� ����������, ��������� ����� (��� �����������
���������, ���� ����� �� ������) �������� � ����������� ����� ������,
� ��������� �������������, ���� �� �� �������������, ��������� ������.
� ��������� ������, ������������� �������� ���������.
- ${��������:+�����}
-
������������ �������������� ��������. ���� �������� �� ���������� ��� �����
������ ��������, �� ������������� ������, � ��������� ������, �������������
��������� �����.
- ${��������:m}
${��������:m:n}����������� ��������. ����������� ������� �������� ���������, ������� � ����������
�������� m � ������ �� �������� ����� n. ���� ����� �� �������, ����������� �������
� ���������� �������� � �� �����. � �������� ����� � �������� �����
�������� �������������� ��������� (��.
������ «���������� �������������� ���������»
����). ����� ������ ���� ��������������� ������. ���� �������� — ������������� �����,
��� ������������� � ����� �������� ���������. ���� �������� — @,
��������� — n ����������� ����������, ������� � ��������� m.
���� �������� — ������ � �������� @ ��� *,
��������� — n ��������� �������, ������� � ${��������[m]}.
���������� �������� ���������� � ����, �� ����������� ����������� ����������,
������� �������������, ������� � 1.
- ${#��������}
-
������������� ����� �������� ��������� � ��������. ���� �������� — * ��� @,
������������� ���������� ����������� ����������. ���� �������� — ������ � ��������
* ��� @, ������������� ���������� ��������� �������.
- ${��������#�����}
${��������##�����}����� ��������������� ��� ������, ����������� ������� ���� ������. ���� ������
�������������� � ������� �������� ���������, �� ����������� ����� �������� ���������,
�� �������� ������� ����� �������� (� ������ #) ��� ����� ������� (� ������
##) ���������. ���� �������� — @ ��� *, �������� �������� ������� �����������
���������� � ������� ������������ ��������� � ������������� ���������� � ����������
������. ���� �������� — ������ � �������� @ ��� *, �������� �������� �������
����������� ���������� � ������� �������� ������� � ������������� ����������
� ���������� ������.
- ${��������%�����}
${��������%%�����}����� ��������������� ��� ������, ����������� ������� ���� ������. ���� ������
�������������� � �������� ������ �������� ���������, �� ����������� ����� �������� ���������,
�� �������� ������� ����� �������� (� ������ %) ��� ����� ������� (� ������
%%) ���������. ���� �������� — @ ��� *, �������� �������� ������� �����������
���������� � ������� ������������ ��������� � ������������� ���������� � ����������
������. ���� �������� — ������ � �������� @ ��� *, �������� �������� �������
����������� ���������� � ������� �������� ������� � ������������� ����������
� ���������� ������.
- ${��������/������/������}
${��������//������/������}������ �������� ���������� �������� ���� ������. ����� ������� ��������� ��������
���������, ��������������� �������, ���������� ��������� �������. � ������ ������,
���������� ������ ������ ���������. �� ������ ������, ���������� ��� ���������
�������. ���� ������ ���������� � #, �� ������ �������������� ������� � ������
�������� ���������. ���� �� ������ ���������� � %, �� ������ �������������� �
���������� �������� ���������. ���� ������ — ������, �������������� ������� ���������
��������� � / ����� ������� ����� �� ���������. ���� �������� — @ ��� *,
�������� ������ ������� ����������� ���������� � ������� ������������ ���������, � �������������
���������� � ���������� ������. ���� �������� — ������ � �������� @ ��� *,
�������� ������ ������� ����������� ���������� � ������� �������� ������� �
������������� ���������� � ���������� ������.
����������� ����������� ���������� ������
����������� ����������� ���������� ������ ��������� ���������,
���������� �������� � ����������� �������� �����, ���������� ������ �����
�������. ������� ��� ����� ����� �����������:
-
$(�������)���
-
`�������`
��������� ������������� bash ������ ��� ����������� ����� ����������
������� � ������ ����������� ����������� ���������� ������������ ���������
������ �������, �� �������� ������� ����������� �������� �����.
���������� �������� ����� �� ���������, �� ��� ����� ���� ������� ��� ���������
�� �����. ����������� ����������� ���������� ������� $(cat file) �����
�������� �������������, �� ����� �������: $(< file).
��� ������������� ������ ����� ����������� � �������� ����������
�������� ����� ���������������� ��� �������, ���� ������ ����� ��� �� ����
������� $, ` ��� . ������ �� �������� ��������, �� ��������������� ��������
�����, ��������� ������������� �������. ��� ������������� ����� $(�������)
��� ������� ����� �������� �������� �������� ������� — �� ���� �� �����
������������ ��������.
����������� ����������� ���������� ������ ����� ���� ���������.
��� ������� ��������� ����������� � ����� � ��������� ����������� ������������
���������� �������� ��������� ��������� ������.
���� ����������� ������ � ������� ��������, ��������� �� ����� � �����������
���� ������ � ����������� �� �����������.
����������� �������� �������������� ���������
��� ���������� �������������� ��������� ������ ��������� ������������� ���������
��� ����������. ��� ���������� �������������� ��������� ������������ ���������
������:.PP
-
$((���������))
��������� �������������� ���, ��� ���� �� ��� ���� ����� � ������� �������, ��
������� ������� � ����� ��������� �� ����� ������������ ��������. �� ����
�������� ��������� ����������� ����������� �������� ����������, ����������� �����,
����������� ����������� ���������� ������ � �������� �������. ����������
�������������� ��������� ����� ���� ����������.
���������� ����������� �� ��������, ������������� ���� � �������
«���������� �������������� ���������«.
���� ��������� �����������, ��������� �������������
bash ������ ��������� � ������������� ���������� � ����������� �� �����������.
����������� ���������
����������� ��������� �������� � ��������, ��������������
����������� ������ (FIFO) ��� ����� ���������� �������� ������ �����
/dev/fd. ��� ����� ��� <(������) ��� >(������). ������� ������ ����������� � ���
������� ��� �������� ����� ����������� � ����������� ������� FIFO ���
����� �� ������ � /dev/fd. ��� ����� ����� ���������� � �������� ���������
������� ������� ��� ��������� �����������. ���� ������������ �����
>(������), ������ � ���� ����� ������ ������� ������ �������� ������. ���� ��
������������ ����� <(������), ����, ���������� � �������� ���������, ����������
������ ��� ��������� ����������� ������ �������� ������.
���� ����������� ��������� ��������������, ��� ����������� ������������ �
������������ �������� ����������, ������������ ����������� ���������� ������
� ����������� �������������� ���������.
��������� �� �����
��������� ������������� ������������ ���������� ����������� �������� ����������,
����������� ���������� ������ � �������������� ����������, �� ������ �
������� �������, �������� �� �� �����.
��������� ������������� ������������� ������ ������ �������� ���������� IFS
��� ����������� � ��������� ���������� ����������� �� ����� �� ���� ��������.
���� ���������� IFS �� ����������� ��� �� �������� —
<������><���������><������� ������>, �.�. �����������, �� �����
������������������ ������ ������ �������� �� IFS ��������� ������������ ����.
���� �� ���������� IFS ����� ��������, ������������ �� ������������,
�� ������������������ ���������� �������� (�������� � �������� ���������)
� ������ � � ����� ����� ������������, ���� ���������� ������� ������ � ��������
IFS (�� ��������� �������� ���������� �������� IFS). ����� ������� � ��������
IFS, �� ���������� �����������, ������ � ������ ������� �� ���� �����������
��������� IFS, ������������ �����. ������������������ ���������� ��������
IFS ����� ��������� ������������. ���� ���������� IFS ����� ������ ��������,
��������� �� ����� �� ����������.
����� ������ ������ («» ��� ») �����������. �� ������ � ������� �������
������ ������, ������������ � ���������� ����������� ����������, �� �������
��������, ���������. ���� ��������, �� ������� ��������, ������������� � �������
��������, ���������� ������ ������, � ��� �������� � ��������� ������.
������, ��� ���� �� ���� �����������, �� � ��������� �� ����� �� �����������.
����������� ���� ������
����� ��������� �� �����, ���� ������ �� ����������� ����� -f,
��������� ������������� bash ������������� ������ ����� � ������� ��������
*, ?, ( � [. ���� ������ ����� �� ���� ��������,
����� ��������� �������� �
���������� ������������� �� �������� ������� ���� ������, ���������������
�������. ���� ��������������� ����� ������ �� ������� � ����� ����������
�������������� nullglob ���������,
����� �������� ��� ���������. ���� �����
nullglob ����������� � ��������������� ����� �� �������, ����� ���������.
���� �������� ����� ���������� ��������������
nocaseglob, �������������
����������� ��� ����� �������� ���������� ��������. ���� ������ ������������
��� ����������� ���� ������, ������ «.» � ������ ����� ��� ����� ����� �����
����� ������ �������������� ����, ���� ������ �� ����������� �����
�������������� dotglob. ��� ������������� ���� ������ ����� �����
������ ������ �������������� ����. � ������ ������� ������ «.» ��������������
��� ����� ������ �������. ��. �������� ����� ���������� ��������������
nocaseglob, nullglob
� dotglob
���� � ������� «���������� ������� ��������������«.
���������� ���������� �������������� GLOBIGNORE ����� ��������������
��� ����������� ��������� ���� ������, ��������������� �������. ����
���������� GLOBIGNORE �����������, ������ ��������������� ��� �����,
��������������� ����� ������ �� ��������, �������� � GLOBIGNORE, ���������
�� ������ ���������������. ����� ������ «.» � «..» ������������ ������, ����
���� ����������� ���������� GLOBIGNORE. ������ ��� ��������� ����������
GLOBIGNORE ������������� ��������������� ����� ��������������
dotglob,
��� ��� ��� ��������� ����� ������, ������������ � «.«, ����� �����������.
��� ��������� ������� ��������� — ������������� ���� ������, ������������ � «.«, —
������� «.*» ��� ���� �� �������� � ���������� GLOBIGNORE.
����� dotglob �����������, ���� ���������� GLOBIGNORE �� �����������.
������������� � ��������
����� ������ � �������, ������������ �� �����������, ��������� ����,
�������������� ���������. ������ NUL � ������� ��������� ������.
����������� ������� ������� ��� ����������� ������������� ����������
�����������.
����������� ������� ������� ����� ��������� ��������:
- *
-
������������� ����� ������, � ��� �����, ������.
- ?
-
������������� ������ ���������� �������.
- […]
-
������������� ������ �� �������� � ���������� �������. ���� ��������,
����������� �������, ������ ��������; ����� ������, ���������� ����������� �����
����� ����� ���������, ������������, ����� ��������������� ���� �����������.
���� ����� ����� [ ���� ������ ! ��� ^, �� ����������� ������������� �����
������, �� ��������� � ���������� �������. ��� ������������� � ������� (—)
��� ���������� ������� � ���������� ������� ������ ��� ���������. ��� �������
����������� ���������� ������ (]), �� ���������� ������� ��� ������ ������ ������.
� ���������� ������� ����� �������� ������ �������� � ������� �����������
[:�����:], ��� ����� — ���� �� ��������� �������, ������������ ����������
POSIX.2:
alnum alpha ascii blank cntrl digit graph lower print
punct space upper xdigit
����� �������� �������������� � ����� ��������, ������������� � ����� ������.
� ���������� ������� ����� �������� ����� ��������������� � ������� �����������
[=c=], ��������������� ���� �������� � ��� �� �������� ����������
(collation weight), ������������ ������� �������, ��� � ������ c.
� ���������� ������� ����������� [.������.] ������������� ������� �
��������� �������� ����������.
���� � ������� ���������� ������� shopt ����������� �����
���������� �������������� extglob, ������������ ��� ��������������
���������� ������������� � ��������. � ��������� �������� ������_�������� —
��� ������ �� ������ ��� ���������� �������� ����� ������������ ����� (|).
��������� ������� ����� ����������� �� ������ ��� ���������� ���������
�����������:
- ?(������_��������)
-
������������� ���� ��� ������ ��������� ��������� ��������
- *(������_��������)
-
������������� ���� ��� ����� ��������� ��������� ��������
- +(������_��������)
-
������������� ������ ��� ������ ��������� ��������� ��������
- @(������_��������)
-
������������� ����� ������ ��������� ��������� ��������
- !(������_��������)
-
������������� ����� ������, ����� ��������������� ������ �� ��������� ��������.
�������� �������
����� ���������� ���� ������������� ���� ����������� ��� �����������������
��������� �������� , ‘ � «, �� ���������� ����������� ���� �����������,
���������.
���������������
����� ����������� ������� �� ������� � �������� ������ ����� ����
�������������� � ������� ����������� �����������, �������������� ���������
���������������. ��������������� ����� ����� ������������ ��� �������� � ��������
������ � ������� ����� ������ ���������� ��������������. ��������� ���������
��������������� ����� �������������� ������� ������� ��� ����������� � �����
�� �����, ��� ����������� ����� �������. ��������������� �������������� �
������� �� ��������, ����� �������.
� ��������� ��������� ���� ����� ����������� ����� �� ������ � ������ ������
��������� ��������������� — <, ����������� ��������������� ������������
�������� ������ (���������� ����� 0). ���� �� ������ �������� ���������
��������������� �������� ������ >, ����������� ��������������� ������������
��������� ������ (���������� ����� 1).
� �����, ������ �� ���������� ��������������� � ��������� ���������, ���� ��
������� �����, ����������� ����������� �������� ������, ������ ������,
����������� �������� ����������, ����������� ����������� ���������� ������,
���������� �������������� ���������, �������� ������� � ����������� ����
������. ���� � ���������� ���������� ����� ������ �����, ���������
������������� bash ������ ��������� �� ������.
������, ��� ������� �������� ��������������� ����� ��������. ��������, �������
-
ls > dirlist 2>&1
�������������� � ���� dirlist ����������� �������� ����� � ����������� �����
������, ����� ��� �������
-
ls 2>&1 > dirlist
�������������� � ���� dirlist ������ ����������� �������� �����, ���������
����������� ����� ������ ��� ��������� ���� ��, ���� � ����������� �������� �����
�� ��� ��������������� � ���� dirlist.
������ ��� �������� ��� �������� ����� �������� � ������ ���������������.
��������������� �������� ������
��������������� �������� ������ �������� � �������� �� ������ �����,
��� �������� ���������� � ���������� ����������� � �����, ����� ����������
����� n, ��� ��� ������������ �������� ������ (���������� ����� 0), ����
n �� �������.
� ����� ������ ��������������� �������� ������ ����� ���:
-
[n]<�����
��������������� ��������� ������
��������������� �������� ������ �������� � �������� �� ������ �����,
��� �������� ���������� � ���������� ����������� � �����, ����� ����������
����� n, ��� ��� ������������ ��������� ������ (���������� ����� 1), ���� n
�� �������. ���� ���� �� ����������, �� ���������; ���� ���������� — ��
��������� �� �������� �������.
� ����� ������ ��������������� ��������� ������ ����� ���:
-
[n]>�����
���� ������������ �������� ��������������� > � � ������� ���������� �������
set ����������� ����� noclobber, ��������������� �� ����� ���������, ����
���� � ��������������� ������ ���������� � �������� ������� ������.
���� ������������ �������� ��������������� >|, ���� ��������
��������������� — > � ����� noclobber � ������� ���������� �������
set ��
�����������, ��������������� �����������, ���� ���� ��������������� ���� ����������.
���������� ����������������� ������
����� ��������������� ������ �������� � �������� �����, ��� �������� ����������
� ���������� ����������� � �����, � ������ ���������� ����� ���������� �����
n, ��� � ���������� � ����������� �������� ����� (���������� ����� 1) ����
n �� �������. ���� ���� �� ����������, �� ���������.
��������������� ������ �� ���������� ����� ��������� ����� ���:
-
[n]>>�����
��������������� ������������ ��������� ������ � ������������ ������ ������
��������� ������������� bash ��������� ������������� ��� ����������� ��������
����� (���������� ����� 1), ��� � ����������� ����� ������ (���������� ����� 2)
� ����, ��� �������� ���������� � ���������� ����������� � �����.
��� ����� ������������ ��� ������� ����������� ���������������:
-
&>������
-
>&�����
������ ������ �������� ����������������. ��� ����������� ������������
������������ ���������:
-
>����� 2>&1
����������� �������� �����
��� ���� ���� ��������������� ��������� ������������� ����� ������ �������
������ �� �������� �����, ���� �� ���������� ������, ���������� ������ ���������
����� (��� ��������� ��������). ��� ����������� �� ����� ������ �����
������������ ��� ����������� ������� ����� ��� �������.
����������� «�������� �����» ����� ��������� ������:
-
<<[-]�����
��������-�����������������
� ����� �� ����������� ����������� �������� ����������, ����������� ����������
������, ���� ������ ��� ���������� �������������� ���������. ���� � ����� ����
��������������� ������, ������������ �������� ����������� �������� ��������
���������� �� �����. ���� � ����� ��� ��������������� ��������, �� ���� �������
«��������� �����» ����������� ����������� �������� ����������, �����������
���������� ������ � ���������� �������������� ���������. � ���� ������
���� <������� ������> ������������, � ���������� ������������ ��� ����������
�������� , $ � `.
���� � �������� ��������� ��������������� ������������ <<-, �� ���
��������� ��������� �� ������� ����� � ������, ���������� ������������,
���������. ��� ��������� ����������� ������� ����������� �����������
«�������� �����» � ��������� ���������� ��������������.
������������ ������������ ������
�������� ���������������
-
[n]<&�����
������������ ��� ������������ ������������ ������� ������. ���� � ����������
����������� ����� ������������ ����� ���� ��� ��������� ����, ����������
����� n ���������� ������ ���������������� ����������� �����. ����
���� � ������������, ���������� ������� �����, �� ������ �� ������, ���������
������ ���������������. ���� ����� ����������� � ����� ���������� —,
���� � ������������ n �����������. ���� n �� �������, ������������
����������� ������� ����� (���������� ����� 0).
��������
-
[n]>&�����
������������ ���������� ��� ������������ ������������ �������� ������. ����
n �� �������, ������������ ����������� �������� ����� (���������� ����� 1).
���� ����� � ����� �� ������ ���������� �����, ��������� �� ������, ���������
������ ���������������. � �������� ������������ ������, ���� n �� ������� � �����
����� ����������� �� ������������ ����� ���� ��� ��������� ����, �����������
�������� ����� � ����������� ����� ������ ���������������� ���, ��� ����
������� ����.
�������� ������������ ������ ��� ������ � ������
�������� ���������������
-
[n]<>�����
�������� �������� �����, ��� �������� ���������� � ���������� �����������
� �����, ��� ������ � ������ ����� ���������� ����� n ��� ����� ����������
����� 0, ���� n �� �������. ���� ���� �� ����������, �� ���������.
����������
���������� ��������� ����������� ������ ������� ����� ������� ������� ������.
��������� ������������� ������������ ������ �����������, ������� �����
������������� � ������� � ������� ���������� ������ alias �
unalias (��. ������
«���������� ������� ��������������» ����). ������ �����
������ �������, ���� ��� �� ����� � �������, ������ � ������ �����������.
���� ��������������� ��������� ������, ����� ���������� ������� ����������.
��� ���������� � ������������� ����� ����� �������� ����� ���������� ������� ������
��� ���������� ��������������, ������� ������������� ����� �����������.
������������ ���������� — ��� ���������� �� ����� ��������� ���� =.
������ ����� ������ ���������� ����� ������ � ������ �����������, �� ���� ���
��������� � ������ �������������� ����������, ��������� ����������� ��
����������. ��� ��������, ��� ����� ������ ��������� ls ��� ls -F,
��������, � bash �� ����� �������� ��������� ����������� ����������� � ������
����������. ���� ��������� ������ ������ ���������� — ������, ��������� ��
����������� ����� �������� ������� ����� ������ � ������ ����������� �,
���� �������, ����������.
���������� ��������� � ��������������� � ������� ������� alias,
� ��������� � ������� ������� unalias.
��� ��������� �������� ���������� � ������������� �����. ���� ����������
���������, ����������� ������� ���������� ��������������.
���������� �� �������������, ���� ��������� ������������� �� ��������
�������������, ���� ������ � ������� ���������� ������� shopt
�� ����������� ����� expand_aliases
(��. �������� ������� shopt ����, � �������
«���������� ������� ��������������«).
������� ����������� � ������������� ����������� ��������� ��������.
��������� ������������� bash ������ ������ �� ������� ���� ����
������ ������ �����, ������ ��� ��������� ����� ��������� � ��� �������.
���������� ������������� ��� ������ ������, � �� ��� �� ����������. �������
����������� ����������, �������� � ����� ������ � ������ ��������, �� �����������,
���� �� ����� ��������� ��������� ������. �������, ������ ����� �����������
���������� � ��� �� ������, ���� ����� ��������� �� ���������. ��� ���������
����� ���������� ��������� ��� ���������� �������. ���������� �������������
��� ������ ����������� �������, � �� ��� �� ����������, ��������� �����������
������� ���� �������� ��������� ��������. ��� ���������, ����������, ������������
� �������, �� �������� �� ��� ���, ���� ������� �� ����������. ��� ��������,
������ ��������� ����������� ����������� � ��������� ������ � �� �����������
������� alias � ��������� ��������.
����������� ��� �������� ������������� ����������� ����������� ���������
���������� ��������������.
�������
������� ���������� ��������������, ������������ ��� ���� ������������ �����
� ������� «��������� ������«, ��������� ��� �������� ������
������������������ ������ ��� ���������� � ����������.
������� ����������� � ��������� �������� ���������� ��������������; ��� ��
������������� �� ��������� ����� ������� (�������� � ����������� ��������
���������� ��������������). ��� ������ ������� �� ��������� ����������
������������ �����������. ����������� �������� # �����������, �������
��� ���������. ����������� �������� 0 �� ����������. ��� ��������� �����������
����� ���������� ���������� �������������� � ������� � ���������� �� �������
���������, ������ ���������� ������� DEBUG (��. �������� ���������� �������
trap � �������
«���������� ������� ��������������» ����) �� �����������.
����� ��������� ��������� ���������� ������� � ������� ���������� �������
local. ������ ���������� � �� �������� ��������� ������������ �������� �
��������� �� ��������.
���� � ������� ����������� ���������� ������� return, ��� �������
�����������, � ���������� ������������ �� ��������� ������� ����� ������
�������. ����� ������� �����������, ����������������� �������� �����������
���������� � ������������ ��������� #, ������� ���� �� ���������� �������.
����� � ����������� ������� ����� �������� � ������� ����� -f ����������
������ declare ��� typeset.
� ������ -F ���������� ������� declare �
typeset ����� �������� ������ ����� �������. ������� �����
��������������, � ������� ����� -f ���������� ������� export,
��� ��� ��� ����� ������������� ���������� � ����������� ���������
���������������.
������� ����� ���� ������������. ������� �������� �� ����������.
���������� �������������� ���������
��������� ������������� � ������������ ������� ������������ ����������
�������������� ��������� (��. ���������� ������� let � ���������
«����������� �������������� ���������» �����).
���������� ����������� �
������� ����� ������ ��� �������� ������������, ���� ������� �� 0
��������������� � �������� ��������������� ��������� �� ������.
��������� � ��������� ������ ������������� �� ������� � ����������
�����������. ������ ����������� � ������� �������� ����������.
- — +
-
������� ����� � ����
- ! ~
-
���������� � ��������� ���������
- **
-
���������� � �������
- * / %
-
���������, �������, ��������� ������� �� �������
- + —
-
��������, ���������
- << >>
-
��������� ����� ����� � ������
- <= >= < >
-
���������
- == !=
-
��������� � �����������
- &
-
��������� �
- ^
-
��������� ����������� ���
- |
-
��������� ���
- &&
-
���������� �
- ||
-
���������� ���
- ���������?���������:���������
-
�������� ����������
- = *= /= %= += -=
-
������������
� �������� ��������� ����� ������������ ���������� ���������� ��������������;
����� ����������� ����������� ����������� �������� ����������. ��������
��������� � �������������� ��������� ���������� � �������� ������.
����� ���������� ���������� �������������� ����� ���� ������������ �
�������������� ����������, �� ������� integer �������������
�� �����������.
��������� � ��������� 0 ���������������� ��� ������������ �����.
��������� 0x ��� 0X ���������� ����������������� ���������.
� ��������� ������� ����� ����� ��� [���������#]n, ��� ��������� —
���������� ����� �� 2 �� 64, �������������� ��������� ������� ���������,
� n — ����� � ���� ������� ���������. ���� ��������� �� �������,
������������ ��������� 10. �����, ������� 9, ��������������
��������� �������, ���������� �������, ��������� _ � @, ������ � ����� �������.
���� ��������� ������ ��� ����� 36, ��������� � �������� ����� ������������ �
������������ ��� ������������� ����� �� 10 �� 35.
��������� ����������� � ������� �����������. ������� ����������� ������������ �
������� �������, ������� ��������� �������������� �������������� ����
����������� ���������� ����������.
�������� ���������
�������� ��������� ������������ ��������� �������� [[ � ����������� ���������
test � [ ��� �������� ��������� ����� � ����������
��������� � ��������������
���������. ��������� ����������� �� ��������� ������� � �������� ������������
�������. ���� ����� �� ���������� ���� � ����� �� ������� ����� ��� /dev/fd/n,
����������� ���� � ������������ n.
- -a ����
-
�������, ���� ���� ����������.
- -b ����
-
�������, ���� ���� ���������� � ������� ����������� ������� �����������.
- -c ����
-
�������, ���� ���� ���������� � �������� ����������� ���������� �����������.
- -d ����
-
�������, ���� ���� ���������� � �������� ���������.
- -e ����
-
�������, ���� ���� ����������
- -f ����
-
�������, ���� ���� ���������� � �������� ������� ������.
- -g ����
-
�������, ���� ���� ���������� � ����� ������������� ��� SGID.
- -h ����
-
�������, ���� ���� ���������� � �������� ������������� ������.
- -k ����
-
�������, ���� ���� ���������� � ����� ������������� «�������» ���.
- -p ����
-
�������, ���� ���� ���������� � �������� ����������� ������� (FIFO).
- -r ����
-
�������, ���� ���� ���������� � �������� ��� ������.
- -s ����
-
�������, ���� ���� ���������� � ����� ��������� ������.
- -t fd
-
�������, ���� ���������� ����� fd ������ � ������ � ����������.
- -u ����
-
�������, ���� ���� ���������� � ����� ������������� ��� SUID.
- -w ����
-
�������, ���� ���� ���������� � �������� ��� ������.
- -x ����
-
�������, ���� ���� ���������� � �������� �����������.
- -O ����
-
�������, ���� ���� ���������� � ����������� ������������, ����������� �������
����������� ��������������� ������������.
- -G ����
-
�������, ���� ���� ���������� � ����������� ������, ���������� �������
����������� ��������������� ������.
- -L ����
-
�������, ���� ���� ���������� � �������� ������������� ������.
- -S ����
-
�������, ���� ���� ���������� � �������� �������.
- -N ����
-
�������, ���� ���� ���������� � ��� ������� � ������� ���������� ������.
- ����1 -nt ����2
-
�������, ���� ����1 ����� (� ������������ � ����� ���������), ��� ����2.
- ����1 -ot ����2
-
�������, ���� ����1 ������, ��� ����2.
- ����1 -ef ����2
-
�������, ���� ����1 � ����2 ��������� �� ����� ���������� � ����� ����������
������ ��������� ������������.
- -o �����
-
�������, ���� ����������� ��������� ����� ���������� ��������������. ������
����� ��. ����� � �������� ����� -o ����������
������� set.
- -z ������
-
�������, ���� ����� ������ — ���� (�.�. ������ — ������).
- -n ������
�������������, ���� ����� ������ — �� ����.
- ������1 == ������2
-
�������, ���� ������ ���������. ������ == ����� ������������ ������ =.
- ������1 != ������2
-
�������, ���� ������ �� ���������.
- ������1 < ������2
-
�������, ���� ������1 � ������� ������ ��� ������������������
������������ ������������ ������2.
- ������1 > ������2
-
�������, ���� ������1 � ������� ������ ��� ������������������
������������ ���� ����� ������2.
- arg1 OP arg2
-
�������� OP — ���� �� ���������: -eq, -ne, -lt,
-le, -gt ��� -ge. ��� �������� �������������� ��������� ����������
������, ���� arg1 �����, �� �����, ������, ������ ��� �����,������, ������ ��� �����
arg2, ��������������. ��������� arg1 � arg2 ����� ���� ������ ������ �������.
��������� ������� �������
��� ��������� ������� ������� ��������� ������������� ������, ����� �������,
��������� �����������, ������������ � ���������������.
- *
-
�����, ���������� �������������� ������������ ��� ������������ ����������
(��, ��� ���� ����� ������ �������) � ���������������, ����������� ���
���������� ���������.
- *
-
� ��������� ������ ����������� ����������� � ������. ���� ����� �����
�������� �����-�� �����, ������ �� ��� ��������� ������ �������, � ��������� —
�� �����������.
- *
-
��������������� ����������� ���, ��� ������� �����
� ������� «���������������«.
- *
-
� ������ ����� = � ������ ������������ ���������� ����� �������������
����������� ������ ������, ����������� �������� ����������, �����������
����������� ���������� ������ � ���������� �������������� ���������, � �����
�������� �������.
���� � ���������� ����� ������� ���, ������������ ���������� ������ �� �����
�������� ���������� ��������������. � ��������� ������, ���������� ����������� �
����� ����������� ������� � �� ������ �� ����� �������� ����������
��������������. ���� ���������� ������� ��������� �������� ���������� � ���������
������ ��� ������, �������� ��������� �� ������ � ������� ����������� �
��������� �������� ������.
���� � ���������� ����� ������� ���, ��������������� �����������,
�� �� ������ �� ����� �������� ���������� ��������������. ��� ������
��������������� ������� ����������� � ��������� �������� ������.
���� ����� ����������� ��� ������� ��������, ���������� ������������ ���,
��� ������� ����. � ��������� ������, ���������� ������� �����������.
��� ���� ���� ����������� ����������� ����������� ���������� ������,
������ ������ ����� ����� ������� ��������� �������, ���������� ������� ����
�����������. ���� ����������� ����������� ���������� ������ �� ����, �������
����������� �� �������� ������ ����.
���������� �������
����� ��������� ������� �� �����, ���� � ���������� ���������� ������� �������
� �������������� ������� ����������, ����������� ��������� ��������..PP
���� ��� ������� �� �������� ������� ����� �����, ��������� �������������
�������� �� �����. ���� ���������� ������� ���������� �������������� �
����� ������, ��� ���������� ��� ���� ������� ����
� ������� «�������«.
���� �� ������� � ����� ������ ���, ��������� ������������� ���� �� � ������
���������� ������. ���� ����� ���������� ������� ����, ��� �����������.
���� ��� �� �������� ������ ������� ��� ������ ���������� ������� � �� ��������
�������, ��������� ������������� bash ������������� ������
������� � �������� ���������� PATH � ������� ������������ ����� �
��������������� ������. ��� ����������� ������ ���� ����������� ������
bash ���������� ���-������� (��. �������� ������� hash
� ������� «���������� ������� ��������������» �����).
������ ����� �� ��������� � PATH
����������� ������ ���� ������� �� ������� � ���� ���-�������.
���� ������� ��� ����� ������ �� �������, ��������� ������������� ������
��������������� ��������� � ��������� ���������� ������� �� �������� ������ 127.
���� ������� ������� ��� ��� ������� �������� ����� �����, ���������
������������� ��������� ��������������� ������� � ��������� ����� ����������.
�������� 0 ��������������� ������ ����� �������, � �� ���������� ���������,
��������������� ���������� � ��������� ������, ���� ��� ������.
���� ��������� ������� �� �������, ������ ��� ���� �� ������������� ��������������
����������� ��������, � ���� ���� �� �������� ���������, ��������������,
��� ���� �������� ��������� ���������� ��������������, ���������� ��� �������.
��� ��� ���������� ����������� ����������� ��������� �������������. ����
����������� ��������� ������������� ��������������������, ��� ���� �� ���
��������� �������� ��� ������ ����� ��������� �������������, �� ������
��������������� ������, ����������� ������������ ��������������� (��. ��������
������� hash ���� � �������
«���������� ������� ��������������«),
���������� ������������.
���� ����� � ����� ���������� � #!, ������� ������ ������ ������ �������������
��� ���������. ��������� ������������� ��������� ��������� �������������
� ������������ ��������, ������� �� ������������ ��������������� ������
������������ ����� ���������������. ����� �������������� � �������� ����������
���������� ���� �������������� ��������, ����� ��� �������������� �� ������
������ ���������, ����� ��� ����� ��������� � �� ���������, ���� ��� ������.
����� ���������� ������
� ���������� �������������� ���� ����� ����������, ���������� ���������
����������:
- *
-
�������� �����, �������������� ��������� ��������������� ��� ������, � ������
���������, ��������� �����������������, ����������� ����������
������� exec
- *
-
������� ������� �������, ������������� ��������� cd,
pushd ��� popd,
��� �������������� ��������� ��������������� ��� ������
- *
-
����� ������ �������� �����, ������������� � ������� ������� umask
��� �������������� �� ������������� ��������
- *
- ������� ����������� ��������, ������������� � ������� trap
- *
-
��������� ���������� ��������������, ������������� ����� ������������
���������� ��� �������������� �� ����� ������������� ��������
- *
-
������� ���������� ��������������, �������� � ���� ���������� ��� ��������������
�� ����� ������������� ��������
- *
-
�����, ������������� ��� ������ (��� �����������, ��� � �������� ���� �
��������� ������) ��� ������������� � ������� ������� set
- *
- �����, ������������� � ������� ������� shopt
- *
- ����������, �������� � ������� ������� alias
- *
-
�������������� ��������� ���������, � ��� �����, ��� ������� �������, ��������
��������� $$ � �������� ��������� $PPID
����� ���������� ��������� �������, �� ���������� ���������� ��� ��������
���������� ��������������, ��� ���������� � ��������� ����� ����������,
��������� �� ��������� ���� �����������. ���� �� ������� �����, ��� ��������
����������� �� ���������� ��������������.
- *
-
�������� ����� ���������� �������������� ���� ����� ��������� � ����������,
�������� ��� ������� �����������������
- *
- ������� ������� �������
- *
- ����� ������ �������� ������
- *
-
���������� ���������� ��������������, ���������� ��� ����������������, � �����
����������, ���������������� ��� ������� � ���������� � �����
- *
-
����������� ������������� � ������������ ��������� �������������� �����������
��������, � �������, � ��� ����������������, ����� ������������.
�������, ��������� � ���� ��������� �����, �� ����� �������� �� �����
���������� ������������� ���������� ��������������.
�������, ���������� ������� ���������� ����������, � ����������� �������
���������� � ������ ����������� ��������� ���������������, ������� ���������
�� ������ �������������, �� ���������� �������� ��������������� �����, ��� ����
������������ ������������ ��������� ��������������� ��� ������.
���������� �������, ���������� ��� ����� ���������, ����� ����������� � �����
������������ ���������� ��������������. ��������� � ����� ������������
���������� �������������� �� ����������� ����� ���������� �������������.
�����
��� ������ ��������� �� ���������� ������ �����, ������� �������� ������.
��� ������ ��� ���-�������� ���� name=value.
��������� ������������� ��������� �������������� ������ ����������� ���������.
��� ������ ��������� ������������� ������������� ���� ����� � �������
����������� �������� ��� ������� �����, ������������� ����������� ��� ���
����������� ���������. ����������� ������� ��������� �����. �������
export � declare -x
��������� ��������� � ������� ��������� � ������� ��
�����. ���� �������� ���������, ��������� � �����, ��������, ����� ��������
���������� ������ �����, ������� ������. �����, ����������� ����������� ��������,
������� �� ��������� ����� ���������� ��������������, �������� ������� �����
���� �������� � ���� ��� ������, �� ����������� ���, ��������� � �������
������� unset, � ����� ����������, ����������� �
������� ������ export � declare -x.
����� ��� ����� ������� ������� ��� ������� ����� ���� �������� ��������� �����
������� ����� ������ ������������ ����������, ��� ������� ����� � �������
«���������«. ��� ��������� ������������ ������
������ �� ����� ���������� �������.
���� ����������� ����� -k, (��. �������� ���������� �������
set ����), �� ��� ��������� � ������������ ��� ������ ����������,
� �� ������ �������������� ����� �������, ���������� � �� �����.
����� ��������� ������������� bash �������� ������� �������, ���������� _
��������������� ������ ������� ����� ����� ������� � ���������� ���� �������
� �����.
������ ������
� ����� ������ ���������� ��������������, �������, ������������� �� ��������
������ 0, ��������� �������. ������ ������ 0 �������� �������� ����������.
��������� ������ ������ �������� �������. ���� ������ ������� �������� ��������,
��������� ������������� bash ���������� � �������� ������� ������
�������� 128+������.
���� ������� �� �������, ����������� ��� �� ���������� ������� ����������
������ ������ 127. ���� ������� �������, �� �� �������� �����������,
������������ ������ ������ 126.
���� ������� �� ���������, ��������� ������ ��������� ��� ������������ ���
����������������, ������������ ������������� ������ ������.
���������� ������� �������������� ���������� ������ 0 (������) ��� ��������
����������, � ��������� ������ (����), ���� ��� ���������� ��������� ������.
��� ������������ ������������� ��� ���������� ������� ���������� ������ 2.
��� ��������� ������������� bash ���������� ������ ������ ��������� �����������
�������, ���� ������ �� ������������ �������������� ������. � ���� ������
�� ��������� ������ � ��������� ��������. ��. ����� �������� ����������
������� exit ����.
�������
����� ��������� ������������� bash �������� � ������������� ������,
��� ���������� ������������ ��������, �� ���������� ������ SIGTERM (��� ���
������� kill 0 �� ���������� ������ ��������������
���������� ��������������),
� ������ SIGINT ��������������� � �������������� (���, ��� ���������� �������
wait �� �� �����������). �� ���� �������,
bash ���������� ������ SIGQUIT.
���� ��������� ���������� ���������, bash ���������� ������� SIGTTIN,
SIGTTOU � SIGTSTP.
� ���������� �������, ���������� ��������� ��������������� bash,
��������������� ����������� ��������, �������������� ��������� ���������������
�� ������������� ��������. ���� ���������� ��������� �� ���������, �����������
������� ����� ���������� ������� SIGINT � SIGQUIT. �������, ����������� ���
����������� ����������� ����������, ���������� ������� ���������� ���������
SIGTTIN, SIGTTOU � SIGTSTP, ��������� � ����������.
�� ��������� ��������� ������������� ��������� ������ ��� ��������� �������
SIGHUP. ����� ����������� �� �������� ������� SIGHUP ���� ��������, ���
�������������, ��� � �������������. ������������� �������� ��������������
���������� ������ SIGCONT, ������������� ��������� ��� ������� SIGHUP. ���
�������������� ������� ��������������� ������� ����������� �������, ���
���������� ������� �� ������� ������� � ������� ����������
������� disown
(��. ������ «���������� ������� ��������������» �����)
��� �������� ��� �� ���������� ������ SIGHUP
� ������� disown -h.
���� � ������� ������� shopt ����������� �����
���������� �������������� huponexit,
bash �������� ������ SIGHUP ���� �������� ��� ����������
������ �������������� ���������� ���������� ��������������.
��� ��������� ��������� ��������������� bash �������, ��� �������� ����������
����������, � �� �����, ����� ��������� ���������� �������, ����������
�� �����������, ���� ������� �� ����������. ����� bash ������� ����������
����������� ������� � ������� ���������� ������� wait,
��������� �������, ��� �������� ���������� ����������, �������� � ������������
���������� ���������� ������� wait �� �������� ������
����� 128 ����� �� ����� ���������� ���� �����������.
���������� ���������
���������� ��������� — ��� ����������� ������������ �������������
(����������������) ���������� ��������� � ���������� (������������)
�� ���������� � ����������. ������������ ������ ������ ��� �����
������������� ���������, ��������� ����������� ��������� ��������� �
��������� ��������������� bash.
��������� ������������� ��������� ������� � ������ ����������. �� ������������
������� ������������� �������, ������� ����� ������������� � �������
������� jobs.
��� ������� ������� ���������� (� ������� ������) bash
������ ������ ���������� ����:
-
[1] 25647
������������, ��� ��� ������� � ������� 1, � ������������� ����������
�������� � ���������, ��������� � ������ �������� — 25647. ��� �������� � �����
��������� �������� ������� ������ �������. ��������� ������������� bash
���������� ������� ������� ��� ������ ��� ���������� ���������.
��� ��������� ���������� ����������������� ���������� ��� ���������� ���������
������� ������������ ������� �������������� ������ ��������� �������� ���������.
����� ���� ������ ��������� (��������, ������������� ������ ��������� �������
����� �������������� ������ ��������� ���������) �������� ������� � ����������,
��������, ������ SIGINT. ��� �������� �������� ������������ (���������� ���������
�����). ������� �������� — ��� ��, ������������� ������ ��������� ������� ��
��������� � ������������; ����� ��������� ������� � ���������� �� ����������.
������ ������������ �������� ����� ������ ������ � ��������� � ��������
������ � ����. ������� ��������, ���������� ������ � ��������� (��� ��������
� ���� ������) �������� �� �������� ��������� ������ SIGTTIN (SIGTTOU),
�������, ���� �� ����������, ���������������� ������ ��������.
���� ������������ �������, � ������� �������� bash, ������������ ����������
���������, bash ��������� ��� ������������. ��� ������� ������� ������������
(������ ^Z, Control-Z) �� ���� ������ ��������, ���� ������� ��������������� �
���������� ������������ ���������� �������������� bash. ��� ������� �������
���������� ������������ (������ ^Y, Control-Y) ������� ��������������� ���
������� ������ ������ � ���������, � ���������� ������������ ����������
�������������� bash. ����� ������������ ����� ��������� ���������� ����� �������,
��������� ������� bg ��� ��� ����������� � ������� ������,
������� fg — ���
����������� � ������������ ������ ��� ������� kill ��� ��� �����������.
������� ^Z ����������� ���������� � ����� �������������� ��������
������ � ���� ������ ������, ��������� ������ � �����.
��������� ������������� ������������ ��������� �������� ��������� �� �������.
������ % �������� ������ ����� �������. �� ������� � ������� n �����
��������� ��� %n. �� ������� ����� ��������� ����� � ������� �������� �����
�������, �������������� ��� ��� ������, ��� ���������, �������� �
��������������� ��������� ������. ��������, %ce ��������� �� �������������
������� ce. ���� ������� ������������� ���������� ��������, bash ������
��������� �� ������. ��������� ���� %?ce, � ������ �������, ��������� �� �����
�������, � ��������� ������ �������� ���������� ��������� ce. ���� ���
��������� ���������� � ���������� ��������, bash ������ ��������� �� ������.
������ %% � %+ ���������� ������� ������� ���������� �������������� —
��������� �������, ������������� ��� ������ � ������������ ������ ��� ���������� �
������� ������. �� ���������� ������� ����� ��������� � ������� ������ %-.
� ����������� ������ ������, ��������� � ����������� ���������, (� ���������,
� ����������� ���������� ������� jobs), ������� ������� ������
���������� ������ +, � ���������� — ������ —.
��� �������� ������� � ������������ ����� ���������� ������ ������ ��� ���:
%1 — ��� ������� ��� ������� «fg %1«, ����������� ������� 1 �� ��������
� ������������ �����. ����������, ������� «%1 &» ���������� ������ �������
1 � ������� ������ � ������������ ������� «bg %1«.
�� ��������� ��������� ������� ��������� ������������� ������ �����.
������ bash ����������� �� ���������� � ��������� ������� ��� ������ ����������
�����������, ����� �� ��������� ���� ����������� ������ ����� ������ �����������.
���� � ������� ���������� ������� set �����������
����� -b, bash ����������� � ����� ���������� ����������.
���� ��� ������� ������������� ������� ���������� ������� ������ �� bash,
��������� ������������� ������ ��������������� ��������������. �����
� ������� ������� jobs ����� ��������� ��������� ���� �������. ����
����� �� ����������� ��������� ������� ��������� ������, ��������� ������������� �� ������
�������������� � ������������� ������� ������������.
�����������
��� ������������� ������ ��������� ������������� bash ������ ���������
�����������, PS1, ����� �� ����� � ����� �������, � ��������� �����������,
PS2, ����� ��� ���������� ������� ���������� �������������� ������.
��������� ������������� bash ��������� ����������� ��� ������ �����������
� ������� ���� ����������� �������� ����� ����������� �������� �� ����������
����������:
-
a
������ ������� ASCII (������ — 07)
d
���� � ������� «����_������ ����� �����» (�.�., «Tue May 26»)
e
����������� ������ ASCII (033)
h
��� ����� ������ �� ������� ������� ‘.‘
H
��� �����
n
������� ������
r
������� �������
s
��� ���������� �������������� — ������� ��� �� ��������� $0 (����� �������
����� ����� ��������� ����� �����)
t
������� ����� � 24-������� ������� ��:��:��
T
������� ����� � 12-������� ������� ��:��:��
@
������� ����� � 12-������� ������� am/pm (����/������)
u
��� �������� ������������
v
������ ���������� �������������� bash (��������, 2.00)
V
����� ���������� �������������� bash, ������ + ������� �����������
(��������, 2.00.0)
w
������� ������� �������
W
������� ��� �������� �������� ��������
!
���������� ����� ������ ������� � ������ �������
#
���������� ����� ������ �������
$
���� ����������� ������������� ������������ — 0, �� #, ����� — $
nnn
������, ��������������� ������������� ����� nnn
\
�������� �����
[
������ ������������������ ���������� ��������, ������� ����� ��������������
��� ��������� � ����������� ����������� �������� ���������
]
���e������ ������������������ ���������� ��������
���������� ����� ������� � ���������� ����� ������� � ������ ������� ������
�����������: ���������� ����� � ������ ������� ������ �� ������� � ������,
������� ����� ��������� �������, ����������� �� ����� �������
(��. ������ «������� ���������� ������» �����), ����� ��� ����������
����� ������� ������ �� ������� � ������ ������, ����������� � ������� ������
���������� ��������������. ����� ������������� ������ ����������� � ���
����������� ����������� �������� ����������, ����������� ����������� ����������
������, ����������� ���������� �������������� ���������, ����������� ����� �
�������� �������, � ����������� �� �������� ����� ���������� ��������������
promptvars
(��. �������� ������� shopt � �������
«���������� ������� ��������������» �����).
���������� READLINE
��� ����������, �������������� ������� ������ ��� ������ ����������
�������������� � ������������� ������, ���� ��� ��� ������ �� ���� �������
����� —noediting. �� ���������, ������� ��������������
������ ������� ������������ � ��������� emacs. ����� ��������������� ��������� ���
�������������� ��������� ������ � ����� ��������� vi. ��� ����������
����������� �������������� ����� ������� ���������� �������������� ������������
����� +o emacs ��� +o vi ���������� ������� set
(��. ������ «���������� ������� ��������������» ����).
���������� �� ������ ������ readline
� ���� ������� ��� ������ ���������� ������ ������������ ���������� � �����
��������� emacs. ����������� ������� ������������ C-�������, ��� ��� C-n
���������� ���������� Control-N. ����������, ����������� ������������
M-�������, ��� ��� M-x ���������� Meta-X. (�� �����������, ��� ��� �������
meta, M-x ���������� ESC x, �.�. ������ ������� Escape � �����
������ ������� x. ��� �����, ������� ESC ��������� ������� meta.
���������� M-C-x ���������� ESC-Control-x, ��� ������ ������� Escape,
�����, ��������� ������� Control, ������ ������� x.)
������� ���������� readline ����� ����� �������� ���������, ������ ������������
���������� ����������. ������, ������, ��� �������� ��������� ���������� ���������.
�������� �������������� ��������� �������, ���������� � ���������� �������
(��������, kill-line) �������� � �� ���������� � ��������������� ������.
�������, ������ ������� ���������� �� ��������� �����, ���������� �������� ����.
���� ������� ������� �����, ���� ����� ����������� ��� ����������� ����������
(�������). �������� ����� ���������� � ��������� �����. ���������������� ��������
��������� �����, �������� ������� �������. �������, �� ��������� �����,
��������� ��������� � ��������� ������ ��������.
������������� ���������� readline
���������� readline ������������� � ������� ������ � ����� �������������
(���� inputrc). � �������� ����� ����� ����� ������� �������� ����������
INPUTRC. ���� ��� ���������� �� �����������, ������������ ����������� ����,
~/.inputrc. ��� ������� ���������, ������������ ���������� readline,
���� ������������� �������� � ��������������� ��������������� ����������
� ������� �������. � ����� ������������� readline ����������� ���� ���������
������� �����������. ������ ������ ������������. ������, ������������ ��������
#, �������� �������������. ������, ������������ �������� $, ���������� ��������
�����������. ��������� ������ ���������� ��������� ������� ������ � ������������
�������� ����������.
����������� ������� ������� ����� �������� � ����� inputrc. ������ ���������,
������������ ��� ����������, ����� ��������� ����������� ������� � �������
�������.
��������, �������� � ���� �������������
-
M-Control-u: universal-argument
���
-
C-Meta-u: universal-argument
�� ����������� � ���������� ������ M-C-u ������� universal-argument ����������
readline.
������������ ��������� ����� ��������: RUBOUT, DEL, ESC,
LFD, NEWLINE, RET, RETURN, SPC, SPACE � TAB. �����
���� ������, ���������� readline ��������� ����������� � �������� ������,
������� ����� ����������� ��� �� ������� (�.�. ������� ����������������).
������� ������� readline
�������� ������ � ����� inputrc �������� � ������� �������� ����������.
���������� ������� ������ ��� ������� ��� ����� ����������������, � �������
�������, � �������� ��� ���� �������. ������� ������� ����� �������� �����
���������: ��� ������������� ��� �������, ��������, � ��������� Meta- ���
Control-, ��� ��� ���������� ������. ��� ������������� �����
���_�������:���_������� ��� ����������������, ���_������� ������ �������� ��
����������. ��������:
-
Control-u: universal-argument
Meta-Rubout: backward-kill-word
Control-o: «> output»
� ���� ������� ���������� ������ C-u ����������� � �������� universal-argument,
M-DEL — � �������� backward-kill-word, � C-o — � �����������������, ���������
� ������ ����� (�.�. �������� � ������� ������ > output � ������).
�� ������ �����, «����������_������»:���_������� ��� ����������������, ����������
������ ���������� �� ��������������� ���� ����� ������� ���, ��� �����
���������� � ������� ��������. ��� ���� ����� ������������ ���������
����������� ������� � ����� GNU Emacs, ��� � ��������� �������.
«C-u»: universal-argument
«C-xC-r»: re-read-init-file
«e[11~»: «Function Key 1»
� ���� �������, ���������� ������ C-u ����� ����������� � ��������
universal-argument. ���������� C-x C-r ����������� � �������� re-read-init-file,
� ���������� ESC [ 1 1 ~ ����� ��������� � ������� ������ Function Key 1.
������ ����� ����������� ������������������� � ����� GNU Emacs ����������� ����.
- C-
-
������� ������� Control
- M-
-
������� ������� Meta
- e
-
����������� ������
- \
-
�������� �����
- «
-
������ »
- ‘
-
������ ‘
������ ����������� ������������������� � ����� GNU Emacs, ������� ��� ����
����� ����������� �������������������, ������������ � �������� �����:
- a
-
�������������� (������)
- b
-
����� (backspace)
- d
-
�������� ������� (delete)
- f
-
������� �������� (form feed)
- n
-
����� ������
- r
-
������� �������
- t
-
�������������� ���������
- v
-
������������ ���������
- nnn
-
������, ASCII-��� �������� ����� ������������ �������� nnn (�� ����� �� ���� ����)
- xnnn
-
������, ASCII-��� �������� ����� ����������������� �������� nnn
(�� ����� �� ���� ����)
��� ����� ������ ���������������� ���������� ��� �� ����������� ���������
��������� ��� ������� �������. ����� ��� ������� ��������� ������ �������. � ����
���������������� ���������������� ������������� ���� �����������
������������������, ������������ � �������� �����. �������� ����� ���������
����� ������ ������ � ������ ����������������, � ��� �����, ������� « � ‘.
��������� ������������� bash ��������� ����������� ��� �������� ������� �������
������� ���������� readline � ������� ���������� ������� bind.
����� �������������� ����� ����������� � ���� ������������� ������ � ������� �����
-o ���������� ������� set
(��. ������ «���������� ������� ��������������» ����).
���������� readline
���������� readline ������������ ����������, ������� ����� ������������ ���
�������������� ��������� �� ������. �������� ���������� ����� ���������� � �����
inputrc ��� � ������� ��������� ����
-
set ���_���������� ��������
�� ����������� ���� ��������� �������, ���������� readline ����� ����� ��������
On ��� Off. ���� ������������ ��� ���������� � �� ����������� ��������:
- bell-style (audible)
-
��������� ���, ��� ����������, ����� ���������� readline ������ ������ ������
(�������� ������) ���������. ���� ������ �������� none, readline ������� ��
������ �������� ������. ���� ������ �������� visible, readline ����������
���������� ������, ���� �� �������������� ����������. ���� ������ ��������
audible, readline �������� ������ �������� ������ ���������.
- comment-begin («#»)
-
������, ����������� ��� ���������� ������� insert-comment ���������� readline.
��� ������� ��������� � �������� M-# � ������ emacs � � ������� # � ������ vi.
- completion-ignore-case (Off)
-
���� ����������� �������� On, ���������� readline ��������� ����������� ���� ������
� ���������� ��������� ������ � ������ ��������.
- completion-query-items (100)
-
����������, ����� � ������������ ����������� ������������� ������ ���� ����������
�������, ������������ �������� possible-completions. ����� �������� �����
��������������� ������������� ��������. ���� ���������� ��������� ����������
������ ��� ����� �������� ���� ����������, � ������������ �����������, ������
�� �� ��� �� �������; ����� ��� ������ �������� �� ��������.
- convert-meta (On)
-
���� ����������� �������� On, readline ����� ��������������� ������� � �������������
������� ����� � ������������������ �������� ASCII ����� ������ �������� ���� �
���������� ������������ ������� (����������, ��������� ����������� ������ ���
����-�������).
- disable-completion (Off)
-
���� ����������� �������� On, readline �� ����� ��������� ���������� ����. �������
���������� ����� ����������� � ������ ���������.
- editing-mode (emacs)
-
��������� ���, � ����� ������� ������� ������ �������� �������� ���������� readline —
����������� emacs ��� vi. ���������� editing-mode ����� ����� �������� emacs ��� vi.
- enable-keypad (Off)
-
���� ����������� �������� On, readline ����� �������� �������� ��������� ���������
����� ���������� ������ ��� ������. ��������� �������� ��� ���������� ��� ���������
������ �� ���������.
- expand-tilde (Off)
-
���� ����������� �������� On, ��� ������� ���������� ���� ����������� ������ ������.
- horizontal-scroll-mode (Off)
-
���� ����������� �������� On, readline ����� ������������ ���� ������ ��� ������,
����������� �� ��� ������������� � ����� ������, ����� �� ����� ��������� ������
������, � �� �������� ��������� �� ����� ������.
- input-meta (Off)
-
���� ����������� �������� On, readline ����� ������������ ���� �������������
�������� (�.�. �� ����� ���������� ������� ��� �������� ��� ������), ����������
�� ����, ������������ �� ������������� ������� ��������. ��� meta-flag — �������
��� ������ ����������.
- isearch-terminators («C-[C-J»)
-
������ ��������, ������� ������ ���������� ������������ ����� ��� ������������
���������� ������� ��� �������. ���� �������� ���� ���������� �� ������, ������������
����� ����� ���������� ������� ESC � C-J.
- keymap (emacs)
-
������������� ������� ��������� ������� ������ ��� readline. ����������� ����� ���������
emacs, emacs-standard, emacs-meta, emacs-ctlx, vi, vi-command � vi-insert.
vi ������������ vi-command; emacs ������������ emacs-standard. ����������� �������� —
emacs; �������� ���������� editing-mode ����� ������ �� ������� �������.
- mark-directories (On)
-
���� ����������� �������� On, � ����������� ������ ��������� ����������� ����� �����.
- mark-modified-lines (Off)
-
���� ����������� �������� On, ����� �������� ������ ������� ������, ������� ���� ��������,
�������� ��������� (*).
- output-meta (Off)
-
���� ����������� �������� On, readline ����� �������� ������� � �������������
������� ����� ���������������, � �� ��� ����������� ������������������,
������������ ������������.
- print-completions-horizontally (Off)
-
���� ����������� �������� On, readline ����� �������� ��������� ����������
��������������� � ���������� ������� �� �����������, � �������, � �� �� ���������,
� ��������.
- show-all-if-ambiguous (Off)
-
�������� ����������� ��������� ������� ����������. ���� ����������� �������� On,
��� ����, ������� ����� ������ ���������� ����������, ������ ���������
�������� �����, � �� ����� ��������� �������.
- visible-stats (Off)
-
���� ����������� �������� On, � ������ ������ ��� ������ ��������� ����������
����������� ������, �������� �� ��� � ������������ � �������� stat(2).
����������� ����������� readline
���������� readline ��������� ��������, ����������� �� ���� ������������
�������� ���������� ������������� ����� C � ����������� ��������� ������� ������� ���
����������� �������� ���������� � ����������� �� �������. ��������������
������ ��������� �����������.
- $if
-
����������� $if ��������� ��������� �� ��� ���� ������� � ����������� �� �������������
������ ��������������, ��� ��������� ��� ����������, ������������� ����������
readline. ����� ������� ������������ �� ����� ������ — ������� ����������� �������
�� �����.
- mode
-
����� mode= ��������� $if ������������ ��� �������� ����, �������� �� ����������
readline � ������ emacs ��� vi. �� ����� ������������ ��������� � ��������
set keymap, ��������, ��� ��������� ������� ������ ��������� emacs �
��������� emacs-ctlx ������ ���� readline ����������� � ������ emacs.
- term
-
����� term= ����� �������������� ��� ��������� ������������� ������� ������
��� ������� ���������, � ���������, ��� �������� ������������������� �������� �
�������������� �������� ���������. ����� ������ �� ������� = ������������ �
������ ������ ��������� � ������ ����� ��������� �� ������� ��������� —.
��� ��������� ��������, ��������, sun ��� ������������� ��� � sun, ��� �
� sun-cmd.
- ����������
-
����������� ���������� ������������ ��� ��������� ������������� ��������
����������. ������ ���������, ������������ ���������� readline, �������������
��� ����������, � � ����������������� ����� ����� ��������� ��������� �������������
��������. ��� ����� ������������ ��� �������� ������ � ��������, ������������ ���
���������� ���������. ��������, ��������� ������� ������ ������������������ ������,
������� � ������� ������� ��� ���������� ����� � bash:
-
$if Bash
# ����� � ������� ������� ��� ���������� �����
«C-xq»: «eb»ef»»
$endif
- $endif
-
��� �������, ��� ����� �� ����������� �������, ��������� ������� $if.
- $else
-
������� � ���� ����� ��������� $if �����������, ���� �� ��������� ������� ��������.
- $include
-
��� ��������� ��������� � �������� ��������� ��� ����� � ������ ������� � ��������
������ �� ����� �����. ��������, ��������� ��������� ������� ��������� ����
/etc/inputrc:
-
$include /etc/inputrc
�����
���������� readline ���������� ������� ��� ������ � ������ ������� ������
(��. ������ «������� ���������� ������» ����) �����, ���������� ������������ ���������. ������� ��� ������ ������: ������������ � �� ������������.
������������ ������ ���������� ������, ��� ������������ �������� ���� ������� ������.
�������� ������� ������� ������� ������, readline ������ ���������
������ �� ������ �������, ��������������� ��� ��������� ���������. ���
������������ ������ ���������� ������ ����� ������� ��������, ������� ����
��� ���������� ������������ �������. ������� �������� ����������
isearch-terminators ������������ ��� ���������� ������������� ������. ����
�������� ���� ���������� �� ������, ������������ ����� ����������� ���������
Escape � Control-J. ������� Control-G ��������� ������������ ����� � ���������������
�������� ������. ��� ���������� ������, ������ �� ������ �������, ���������� ������
������, ���������� ������� �������. ��� ������ ������ ��������������� ������� �
������ �������, ������� Control-S ��� Control-R, � ����������� �� �����������
������. � ����������, ����� �������� ����� � �������� ��� ������ �����������
��������� ������, ��������������� ��� ���������� ��������� ������. ���� ����� ������
������������������ ��������, ����������� � ������� ���������� readline,
�������� � ���������� ������ � ���������� �������. ��������, ������� ������� Enter
�������� ����� � �������� ��������� ������� �� ������ �������.
��� ������������� ������ ����� ������� ������ �������� ��� ������� ������. ������� ������
����� ���� ������� ������������� ��� ���� ������ ����������� ������� ������.
����� ������ readline
���� ����������� ������ ���� ������ � ����������� ���������� ������, �
������� ��� ���������. ����� ������, ��� ������� �� ������� ����������
������, �� ��������� �� � ����� ���������� �� ���������.
������� ��� �����������
- beginning-of-line (C-a)
-
���������� ������ � ������ ������� ������.
- end-of-line (C-e)
-
���������� ������ � ����� ������.
- forward-char (C-f)
-
���������� ������ �� ���� ������ ������.
- backward-char (C-b)
-
���������� ������ �� ���� ������ �����.
- forward-word (M-f)
-
���������� ������ �� ���� ����� ������. ����� ������� �� ���������-��������
�������� (���� � ����).
- backward-word (M-b)
-
��������� � ������ �������� ��� ����������� �����. ����� ������� ��
����������������� �������� (���� � ����).
- clear-screen (C-l)
-
������� �����, �������� ������� ������ ������ ������. ��� �������� ���������
��������� ������� ������, �� ������ ������.
- redraw-current-line
-
��������� ������� ������.
������� ��� ������ �� ������� �������
- accept-line (Newline, Return)
-
��������� ������, ���������� �� ������� �������. ���� ��� ������ — �� ������,
��������� �� � ������ ������� � ������������ � ���������� ����������
HISTCONTROL. ���� ������ ������������ ����� ���������� ������ �� ������
�������, �� ������ ������� ����������������� � �������� ���������.
- previous-history (C-p)
-
��������� ���������� ������� �� ������ �������, ����������� � ������ ������.
- next-history (C-n)
-
��������� ��������� ������� �� ������ �������, ����������� � ����� ������.
- beginning-of-history (M-<)
-
��������� � ������ ������ � ������ �������.
- end-of-history (M->)
-
��������� � ����� ������� ��������� ������, �.�. � ������� �������� ������.
- reverse-search-history (C-r)
-
���� ������� ����� ����������, ������� � ������� ������ � �����������, ���
�������������, � ������ ������ �������. ���� ����� — ������������.
- forward-search-history (C-s)
-
����� �����, ������� � ������� ������ � �����������, ��� �������������,
� ����� ������ �������. ���� ����� — ������������.
- non-incremental-reverse-search-history (M-p)
-
�������������� ����� ��������� ������������� ������ � �������� �����������,
������� � ������� ������.
- non-incremental-forward-search-history (M-n)
-
�������������� ����� ��������� ������������� ������ �����,
������� � ������� ������.
- history-search-forward
-
����� ����� � ������ ������� ������ �������� �� ������ ������� ������ ��
������� ������� (������� �������). ���� ����� — �� ������������.
- history-search-backward
-
����� � �������� ����������� � ������ ������� ������ �������� �� ������
������� ������ �� ������� ������� (������� �������). ���� ����� — �� ������������.
- yank-nth-arg (M-C-y)
-
��������� ������ �������� ���������� ������� (������ ��� ������ �����
���������� ������) � ������� ������� �������. ���� ������ �������� n,
��������� n-��� ����� ���������� ������� (����� � ���������� ������� ����������
� 0). ���� �������� — �������������, ����������� n-��� ����� �� �����
���������� �������.
- yank-last-arg (M-., M-_)
-
��������� ��������� �������� ���������� ������� (��������� ����� � ����������
������ ������ �������). ���� ������ ��������, �������� ��� ��, ��� �
������� yank-nth-arg. ��� ���������������� ������ �������
yank-last-arg ����������� ������ � �������� ����������� �� ������ �������,
�� �������� ���������� ��������� ��������� ������.
- shell-expand-line (M-C-e)
-
��������� ����������� � ������ �� �������� � ��������� ���������������.
����������� ����������� ����������� � ������ �� ������ �������,
� ����� ����������� ����. �������� ����������� ������ �� ������ �������
��. ����� � ������� «����������� ������ �� ������ �������«.
- history-expand-line (M-^)
-
��������� ����������� ������ �� ������ ������� � ������� ������.
�������� ����������� ������ �� ������ �������
��. ����� � ������� «����������� ������ �� ������ �������«.
- magic-space
-
��������� ����������� ������ �� ������ ������� � ������� ������
� ��������� ������. �������� ����������� ������ �� ������ �������
��. ����� � ������� «����������� ������ �� ������ �������«.
- alias-expand-line
-
��������� ����������� ����������� � ������� ������. �������� �����������
����������� ��. ����� � ������� «»����������«.
- history-and-alias-expand-line
-
��������� ����������� ����������� � ������ �� ������ ������� � ������� ������.
- insert-last-argument (M-., M-_)
-
������� ��� ������� yank-last-arg.
- operate-and-get-next (C-o)
-
��������� ������� ������ ��� ���������� � ��������� ��������� ������
����� ������� �� ������ ������� ��� ��������������. ��������� ������������.
������� ��� ��������� ������
- delete-char (C-d)
-
������� ������ � ������� �������. ���� ������� ������� — ������ ������,
� ������ ��� �������� � ��������� ������� ������� �� ��������� � �������
delete-char, ���������� EOF.
- backward-delete-char (Backspace)
-
������� ������ ����� ��������. ��� �������� ��������� ���������
��������� ��������� ����� � ��������� ������ (kill ring).
- forward-backward-delete-char
-
������� ������ � ������� �������, ���� ������� �� � ����� ������, — �
���� ������ ��������� ������ ����� ��������. �� ��������� ��� ������� �� �
����� ���������� ������ �� ���������.
- quoted-insert (C-q, C-v)
-
��������� ��������� ��������� ������ ���������. ��� ����� ���������, ��������,
������� ���� C-q.
- tab-insert (C-v TAB)
-
��������� ������ ���������.
- self-insert (a, b, A, 1, !, …)
-
��������� ��������� ������.
- transpose-chars (C-t)
-
��������� ������ ����� ������� �������� �� ������� �������. ������� �������
����� ������������ ������ �� ���� ������. ���� ������� ������� — ����� ������,
������ ������� ��� ������� ����� ������� ��������. ������������� ��������� ��
��������������.
- transpose-words (M-t)
-
��������� ���������� ����� �� �����, �� ������� (��� ����� ��������) �����
������. ������ ������������ � ����� ������������� �����.
- upcase-word (M-u)
-
��������� � ������� ������� ������� (��� ��������� �����). ���� ������
������������� ��������, ��������� � ������� ������� ���������� �����, �� ��
������ ������� �������.
- downcase-word (M-l)
-
��������� � ������ ������� ������� (��� ��������� �����). ���� ������
������������� ��������, ��������� � ������� ������� ���������� �����, �� ��
������ ������� �������.
- capitalize-word (M-c)
-
�������� � ��������� ����� ������� ��� ��������� �����. ���� ������
������������� ��������, �������� � ��������� ����� ���������� �����, ��
�� ������ ������� �������.
�������� � �������
- kill-line (C-k)
-
������� ����� �� ������� ������� ������� �� ����� ������.
- backward-kill-line (C-x Backspace)
-
������� ����� �� ������ ������.
- unix-line-discard (C-u)
-
������� ����� �� ������� ������� �� ������ ������. ��������� ����� ���������� �
��������� �����.
- kill-whole-line
-
������� ��� ������� ������� ������, ���������� �� ������� �������.
- kill-word (M-d)
-
������� ������� �� ������� �� ����� ����� ���, ���� ������ ����� ����� �������, ��
����� ���������� �����. ������� ���� ������������ ��� ��, ��� � ���
������� forward-word.
- backward-kill-word (M-Backspace)
-
������� ����� ����� ��������. ������� ����� ������������ ��� ��,
��� � � ������� backward-word.
- unix-word-rubout (C-w)
-
������� ����� ����� ��������, ��������� � �������� ������������ ���� ����������
�������. ������� �����, ����� �������, ���������� �� �������� � �������backward-kill-word.
- delete-horizontal-space (M-)
-
������� ��� ������� � ������� ��������� ������ ������� �������.
- kill-region
-
������� ���� ����� �� ������� ������� �� ������� (����������� �������
�������). ���� ����� �������� �������� (region).
- copy-region-as-kill
-
�������� ����� ������� � �����.
- copy-backward-word
-
�������� ����� ����� ������� �������� � �����. ������� ���� ������������ ��� ��,
��� � ������� backward-word.
- copy-forward-word
-
�������� ����� ����� ������� ������� � �����. ������� ���� ������������ ��� ��,
��� � ������� forward-word.
- yank (C-y)
-
��������� ������� ������� ���������� ������ � ������� �������.
- yank-pop (M-y)
-
������������ ��������� ����� � ��������� ����� ������� �������. ��������
������ ����� ������� yank ��� yank-pop.
�������� ���������
- digit-argument (M-0, M-1, …, M—)
-
��������� ��������������� ����� � ��� ������������ ��������� ��� ��������
����� ��������. M— �������� ������������� ��������.
- universal-argument
-
��� ��� ���� ������ ������� ���������. ���� ����� ���� ������� ���� ����
��� ��������� ����, ��������, ������������ �� ����� �����, ��� ����� ��������
��������. ���� ����� ������� ���� �����, ��������� ����������
universal-argument ��������� �������� ��������, � ��������� ������ —
������������. � �������� ������������ ������, ���� ����� ����� ������� ����
������, �������� �� ����� ��� ����� �����, �������� count ��� ���������
������� ���������� �� ������. �������� count ������������� ����� �������� 1,
��� ��� ��� ������ ���������� ���������� ������, ����� — ����������� � ��� �����.
����������
- complete (TAB)
-
�������� ��������� �����, ��������� �� ������� �������.
��������� ������������� bash �������� ��������� ����������,
������������ ����� ��������������� ��� ��������� � ����������
(���� ����� ���������� � $), ��� ��� ������������ (���� ����� ����������
� ~), ��� ��� ����� (���� ����� ���������� � @), ��� ��� �������
(������� ���������� � �������). ���� �� ����� �� ���� �������� ���������
������ �� �������, ����������� ������� ���������� ����� �����.
- possible-completions (M-?)
-
������ ��������� ���������� ������ �� ������� �������.
- insert-completions (M-*)
-
��������� ��� ���������� ���������� ������, ������������ ��������
possible-completions.
- menu-complete
-
���������� ������� complete, �� �������� ����������� �����
������ �� ��������� ����������. ��������� ���������� �������
menu-complete ��������� ��������� ������� �� ������ ��������� ����������.
� ����� ������ ���������� �������� ������ � ����������������� ��������
�����. �������� n �������� ������ �� n ������� ������ � ������ ����������;
������������� �������� �������� ������� �� ������ � �������� �����������.
�������������� �������� ���� ������� � ������� TAB, �� ���������� ��� �� �
����� ���������� ������ �� ���������.
- delete-char-or-list
-
������� ������ � ������� �������, ���� ������ �� ��������� � ������
��� � ����� ������ (���������� ������� delete-char). ���� ������ — � �����
������, ����� ���� ���������� ������� possible-completions. ���
������� ���������� �� � ����� ���������� ������ �� ���������.
- complete-filename (M-/)
-
�������� ��������� ��������� ����� �� ������� ������� ��� ��� �����.
- possible-filename-completions (C-x /)
-
������ ��������� ���������� ������ �� ������� �������, ����������������� ���
��� �����.
- complete-username (M-~)
-
�������� ��������� ��������� ����� �� ������� ������� ��� ��� ������������.
- possible-username-completions (C-x ~)
-
������ ��������� ���������� ������ �� ������� �������, ����������������� ���
��� ������������.
- complete-variable (M-$)
-
�������� ��������� ��������� ����� �� ������� ������� ��� ��������� �
���������� ���������� ��������������.
- possible-variable-completions (C-x $)
-
������ ��������� ���������� ������ �� ������� �������, ����������������� ���
��������� � ���������� ���������� ��������������.
- complete-hostname (M-@)
-
�������� ��������� ��������� ����� �� ������� ������� ��� ��� �����.
- possible-hostname-completions (C-x @)
-
������ ��������� ���������� ������ �� ������� �������, ����������������� ���
��� �����.
- complete-command (M-!)
-
�������� ��������� ��������� ����� �� ������� ������� ��� ��� �������.
��� ���������� ������ ����� �������������� ��������������� � ������������,
������������������ �������, ��������� ���������� ��������������,
����������� ��������� ���������� �������������� �, �������,
� ������� ����������� ������.
- possible-command-completions (C-x !)
-
������ ��������� ���������� ������ �� ������� �������, ����������������� ���
��� �������.
- dynamic-complete-history (M-TAB)
-
�������� ��������� ��������� ����� �� ������� �������, ��������� ���
�� �������� �� ������ ������� ����������� ������ � ������� ���������
������������.
- complete-into-braces (M-{)
-
��������� ���������� ����� ����� � ���������� ������ ��������� ����������
� �������� �������, ����� �� ��� ���� ��������� ��������� ���������������
(��. ������ «����������� �������� � �������� �������» �����).
������������ �������
- start-kbd-macro (C-x ()
-
�������� ����������� ��������� �������� � ������� ������������ ������.
- end-kbd-macro (C-x ))
-
��������� ����������� ��������� �������� � ������� ������������ ������ �
��������� ��� �����������.
- call-last-kbd-macro (C-x e)
-
��������� ��������� ������������ ������������ ������, ������ �������
������� ���, ��� ���� �� ��� ���� ������� � ����������.
������ �������
- re-read-init-file (C-x C-r)
-
������������ ���������� ����� inputrc � ��������� ��� ��������� � ���
�������� ������ � �������� ����������.
- abort (C-g)
-
��������� ������� ������� �������������� � ������ ������ ���������
(��� ������� �������� �������� bell-style).
- do-uppercase-version (M-a, M-b, M-x, …)
-
���� ������ x, ��������� ������ � �������� Meta, �������� �������� �������
��������, ��������� �������, ����������� � ���������������� �������
� ������� ��������.
- prefix-meta (ESC)
-
������������� ��������� ��������� ������ ��� ������������ �������� Meta.
ESC f ������������ Meta-f.
- undo (C-_, C-x C-u)
-
������������ ������, ������������ �������� ��� ������ ������.
- revert-line (M-r)
-
������ ���� ��������� ������� ������. ���������� ���������� �������
undo ��������� ��� �� ����������� ������ � �������� ���������.
- tilde-expand (M-&)
-
��������� ������ ������ � ������� �����.
- set-mark (C-@, M-<space>)
-
������������� ����� � ������� �������. ���� ����� �������� ��������, �����
��������������� � ��������������� �������.
- exchange-point-and-mark (C-x C-x)
-
������ ������� ����� � ������� �������. ������� �������� ������� ����������
����������� � �����, � ������� ������� ������� ������������ � �����.
- character-search (C-])
-
�������� ������, � ������ ����������� �� ��������� ��������� �����
�������. ������������� �������� �������� ����� ����������� ���������.
- character-search-backward (M-C-])
-
�������� ������, � ������ ����������� �� ���������� ��������� �����
�������. ������������� �������� �������� ����� ���������� ���������.
- insert-comment (M-#)
-
�������� ���������� comment-begin ���������� readline ����������� �
������ ������� ������, � ������ �����������, ��� ���� �� ���� ������
������� Enter. � ����������, ������� ������ ���������� ������������
���������� ��������������.
- glob-expand-word (C-x *)
-
����� ����� ������� �������� ��������������� ��� ������ ���� ������ �
������ ���� ����������� ������ ��������������� ���� ������.
- glob-list-expansions (C-x g)
-
�������� ������ ���� ������, ������������ ��������
glob-expand-word, � ����� ����� �������� ������.
- dump-functions
-
������ ��� ������� � ���������� ������, � ������� ��� ���������, �
�������� ����� readline. ���� ����� �������� ��������,
��������� ������������� ���, ��� ��� ����� ��������������� �������� � ���� inputrc.
- dump-variables
-
������ ��� ���������� ���������� readline ������ � �� ���������� �
�������� ����� readline. ���� ����� �������� ��������,
��������� ������������� ���, ��� ��� ����� ��������������� �������� � ���� inputrc.
- dump-macros
-
������ ��� ���������� ������ readline, ����������� � ��������, �
������, ������� �������� ��� �� �������. ���� ����� �������� ��������,
��������� ������������� ���, ��� ��� ����� ��������������� �������� � ���� inputrc.
- display-shell-version (C-x C-v)
-
������ ���������� � ������ �������� ���������� ���������� �������������� bash.
������� ���������� ������
��� ��������� ����� -o history � ������� ���������� ������� set,
��������� ������������� ������������ ������ � ������� ����������
������ — ������ ����� ��������� ������. ����� ���������
HISTSIZE ������ (�� ��������� — 500) ����������� � ������ �������.
��������� ������������� ��������� � ������ ������� ������� �� �����������
�������� ���������� � ���������� (��. ������ «������» �����),
�� ����� ����������� ������ �� ������ �������, � ������ �������� ����������
���������� �������������� HISTIGNORE � HISTCONTROL. ��� ������� �������
���������� ������ ����������� �� �����, ��� �������� �������� ����������
HISTFILE (���������� — ~/.bash_history). ���� HISTFILE ��� �������������
���������, ����� � ��� ���� �� ����� HISTFILESIZE �����. ��� ���������� ������
�������������� ���������� �������������� ���������
HISTSIZE ����� ���������� �� ������ ������� ���������� ������
� ���� HISTFILE. ���� ����������� ����� ���������� ��������������
histappend (��. ��������
������� shopt � �������
«���������� ������� ��������������» ����),
������ ����������� � ����� �������,
����� ���� ������� ����������������. ���� ���������� HISTFILE �� �����������
��� ���� ������� �� �������� �� ������, ������� ���������� ������ ��
�����������. ����� ���������� ������� ���� ��������� �� �� ����� ���
HISTFILESIZE �����. ���� ���������� HISTFILESIZE �� �����������
�������� ����� �� �����������.
���������� ������� fc (��. ������
«���������� ������� ��������������» ����)
����� �������������� ��� ������ ��� �������������� � ���������� ����������
����� ������ �������. ���������� ������� history ���������
�������� � �������� ������ ������� ���������� ������, � �����
��������� ������ �������. ��� �������������� ��������� ������ �� ���� �������
�������������� �������� ������� ������ �� ������ ������� ���������� ������.
��������� ������������� ��������� ��������� ���, ����� ������� ����������� �
������ �������. ���������� HISTCONTROL � HISTIGNORE ��������� ����������
��������� ����������� ������. ����� ���������� ��������������
cmdhist, ���
���������, ���������� ��������� ������������� �������� ��������� ��� ������
������������� ������� � ����� ������ ������ �������, �������� ��� �������������
����� � ������� ��� ����������� �������������� ������������. �����
���������� �������������� lithist ���������� ��������� ������� ��
����������� ���������� ������ ������ ������� ����� � �������. ��������� � �����
����� ���������� �������������� ����������� ���� � �������� ����������
������� shopt � �������
«���������� ������� ��������������«.
����������� ������ �� ������ �������
��������� ������������� bash ������������ ����������� �����������
������ �� ������ �������, ���������� ���������� �������������� csh.
� ���� ������� ������ ��������������� ���������. ��� �����������
���������� �� ��������� ��� ������������� ��������� ���������������
� ����� ���� ��������� � ������� ����� +H ���������� �������
set (��. ������
«���������� ������� ��������������» �����).
�� ������������� ��������� �������������� �� ��������� �� ���������
����������� ������ �� ������ �������.
� ������� ���� ����������� ����� �� ������ ������� ����� ���������
�� ������� �����, ��� �������� ���������� ������, ������� ����������
���������� ������� � ������� ������ � ����������� ������ � ����������
��������.
����������� �� ������ ������� ����������� ����� �� ����� ����������
���� ������, ������ ��� ��������� ������������� �������� �� �� �����.
��� ����������� � ��� ������. ������� ������������, ����� ������
�� ������ ������� ������������ ��� �����������. ����� �� ���� ������
���������� ����� ��� ������� � ������� ������. ������, ���������� ��
������ �������, — ��� �������, � �� �����, � �������� ����������
������, — �����. ��� ��������������� ���������� ������� ��������������
��������� ������������. ������ ����������� �� ����� ����� ��� ��, ���
� ��� ������ �������, ��� ��� ��������� ����, �����������
������������� � ������ � �������, ��������� ����� ������.
����������� �� ������ ������� ������������ ������������ ����������� ��
������ �������. �� ��������� — ��� ������ !. �������������
���� ���������� ����� ������ �������� ����� () � ����������
���������.
������������� ����������� �� ������ ������� ����� ��������� �����
��������� ���� ����� ���������� �������������� � ������� ����������
������� shopt. ���� ����������� ����� ���������� ��������������
histverify
(��. �������� ���������� ������� shopt) � ������������
���������� readline, ���������� ����������� �� ������ ������� ��
���������� ����� ����������� ���������� ��������������. ������ �����,
���������� � ���������� ����������� ������ ����� ����������� � �����
�������������� readline ��� ����������� ���������. ���� ������������
���������� readline � ����������� ����� ���������� ��������������
histreedit, ����������� ����������� �� ������ ������� ������
����� �������� ��������� � ����� �������������� readline ���
�����������. ����� -p ���������� ������� history
����� �������������� ��� ���������������� ��������� ����������� �����������
�� ������ ������� ����� �� ��������������. ����� -s ���������� �������
history ��������� ��������� ������� �
����� ������ ������� ��� �� ������������ ����������, ��� ��� �� ����� ����� ��������
������������ � ����������.
��������� ������������� ��������� ��������� ����������� ���������
������������, ������������ ���������� ����������� ������ �� ������
������� (��. �������� ���������� histchars ����� � �������
«���������� ���������� ��������������«).
������� �������
������� ������� — ��� ������ �� ������� ��������� ������ � ������
�������.
- !
-
�������� ����������� �� ������ ����������� ������, ���� ������
����� �� ���� ������, ������� ������, = ��� (.
- !n
-
������ �� ��������� ������ n.
- !-n
-
������ �� ������� ������ ����� n.
- !!
-
������ �� ���������� �������. ��� ������� ��� ������� !-1.
- !������
-
������ �� ����� �������� �������, ������������ �� ������.
- !?������[?]
-
������ �� ����� �������� �������, ���������� ������. ����������� ������
? ����� �� ���������, ���� ����� ����� ������ ���� ����� ������.
- ^������1^������2^
-
������� �����������. ��������� ��������� �������, �������
������1 �������2. ���������� !!:s/������1/������2/
(��. ��������� «������������» ����).
- !#
-
��� ��������� �� ����� ������� ��������� ������.
������� ����
������� ���� (word designators) ������������ ��� ������ ����������� ���� �� �������.
��������� (:) �������� ������� ����� �� ������������ �������. ��������� �����
�� ���������, ���� ������� ����� ���������� ��������
^, $, *, — ��� %. ����� ���������� � ������ ������,
������ ������ ����� ����� ����� 0. ����� ����������� � ������� ������ ����� ���� ������.
- 0
-
������� �����. ��� ���������� �������������� ��� — ��� �������.
- n
-
n-��� �����.
- ^
-
������ ��������. �.�. ����� 1.
- $
-
��������� ��������.
- %
-
�����, ��������������� ��� ��������� ������ ‘?������?‘.
- x-y
-
�������� ����; ‘-y‘ — ��� ���������� ��� ‘0-y‘.
- *
-
��� �����, ����� ��������. ��� ������� ‘1-$‘.
������ ��������� ������������ * ���� � ������� ���� ����� ���� ����� —
� ���� ������ ������������ ������ ������.
- x*
-
���������� ��� x-$.
- x-
-
����������� ����� ��� x-$, ���������� x*, �� �� ������� ��������� �����.
���� ������� ����� ������� ��� ������������ �������, � ��������
������� ������������ ���������� �������.
������������
����� �������������� ������� ����� ����� ���� ������������������ ������ ���
���������� ��������� �������������, ������������ ���������� — ‘:‘.
- h
-
������� ��������� ��������� ����� �����, �������� ������ ������.
- t
-
������� ��� ��������� ���������� ����� �����, �������� ������ ���������.
- r
-
������� ��������� ������� ���� .xxx, �������� ������� ��� �����.
- e
-
������� ���, ����� ���������� ��������.
- p
-
������ ����� ������� �� �����, �� �� ��������� ��.
- q
-
����� ������������� ����� � ������� ��� �������������� ���������� �����������.
- x
-
����� ������������� ����� � �������, ��� � ����������� q, �� ��������� ��
����� �� �������� � �������� ����� ������.
- s/old/new/
-
����������� new ������ ������� ��������� old � ������ �������. ������
/ ����� ������������ ����� ������������. ����������� ������������ ��
����������, ���� �� �������� ��������� �������� � ������ �������.
������������ � ������� old � new ����� ����������� �������� �����.
���� � ������ new ���� ���������� &, �� ���������� ������� old.
�������� ����� ��������� ���������� &. ���� ������ old — ������,
������ ��� ������������ ������ �� ��������� �� ������� ����������� ���,
���� ����������� �� ������ ������� ����� �� ����, ��������� ������,
���������� � ������� ����������� !?������[?].
- &
-
��������� ���������� �����������.
- g
-
��������� ��������� �� ���� ������ �������. ���� ����������� ������������
��������� � ‘:s‘ (��������, ‘:gs/old/new/‘) ��� ‘:&‘. ��� �������������
� ‘:s‘, ������ ����� (/) ����� ��������� ����� ������������, � �����������
������������ ��������� �� �����������, ���� �� �������� ��������� ��������
� ������ �������.
���������� ������� ��������������
���� ���� �� ������� �����, ������ �� ��������� � ���� �������
���������� ������, ����������� �����, ������������ � ������ (—),
���������� ��� ������ (—) ��� ������� ���������� �����.
- : [���������]
-
�������� �������; ������� �� ������ ������,
����� ����������� ���������� � ���������� ��������� ���������������.
���������� ������� ��� ��������.
- . ���_����� [���������]
source ���_����� [���������]������ � ��������� ������� �� ���������� ����� � ����� �������� ����������
�������������� � ���������� ������ ������ ��������� ����������� ������� ��
�����. ���� ���_����� �� �������� ����� �����, ���� ������ � ���������,
������������� � ���������� PATH. ������� ���� �� ����������� ������ ����
�����������. ���� �� � ����� �� ���������, ������������� � PATH, ���� �
��������� ������ �� ������, �� ������ � ������� ��������. ���� ���������
����� sourcepath ����������
������� shopt, ������������� � PATH �������� ��
���������������. ���� �������� ���������, ��� ���������� ������������
����������� ��� ���������� �����. ����� �������� ����������� ���������� ��
����������. �������� ������ ���������� ������ ��������� ����������� �
�������� ������� (������ ������ ����� 0, ���� �� ���� ������� �� �����������),
��� 1, ���� ���� �� ������ ��� �� ����� ���� ��������.
- alias [-p] [���[=��������] …]
-
������� alias ��� ���������� ��� � ������ -p ������ ������ ����������� ������ �
���� alias ���=�������� � ����������� �������� �����. ���� ��������
���������, ������ ���, ��� �������� ������ ��������, ���������� �����������.
���� �������� ����������� ��������, � ��������� ����� ��� ����������� �����
����������� ������ ����������. ��� ������� ����� � ������ ���������� ���
���������������� �������� �������� ��� � �������� ����������������
����������. ������� alias ���������� ������, ���� ������ �� ������ ���, ��
���������� ������ ����������.
- bg [�������]
-
������������ ���������� ���������� ������� � ������� ������, ��� ���� �� ���
���� �������� � ������������ &. ���� ������� �� �������, � ������� �����
����������� ������� ������� ���������� ��������������. ������� bg �������
���������� 0, ���� ������ ���������� ��������� �� ��������� ���, ���
���������� ���������� ���������, ��������������� ������� �� ������� ���
����������� ��� ����������� ���������� ���������.
- bind [-m keymap] [-lpsvPSV]
-
bind [-m keymap] [-q �������] [-u �������] [-r keyseq]
bind [-m keymap] -f ���_�����
bind [-m keymap] keyseq:���_������������� ������� �������� ������ � ������� ���������� readline ��� �����������
���������� ������ � ������� ��� ������� readline. ��� ���� ��������������
����� �� ��������� ��������, ��� � � ����� .inputrc, �� ������ �������� ������
������������ ��� ��������� ��������; �.�. ‘»C-xC-r»: re-read-init-file’.
�����, ���� ��� ��������, ����� ��������� ��������:
- -m keymap
-
������������ ����� �������� ��� ����������� ��������. ����������� �����
�������� emacs, emacs-standard, emacs-meta, emacs-ctlx,
vi, vi-command � vi-insert. vi
������������ vi-command; emacs ������������ emacs-standard.
- -l
-
������ ���� ���� ������� readline.
- -p
-
������ ����� ������� � �������� readline ���, ����� �� ����� ���� ���������
��������.
- -P
-
������ ������� ����� ������� � �������� readline.
- -v
-
������ ����� � �������� ���������� readline ���, ����� �� ����� ����
��������� ��������.
- -V
-
������ ����� � �������� ������� ���������� readline.
- -s
-
������ ���������� ������ readline, ����������� � ��������, � ���������� ���
�� ������� ������ ���, ����� �� ����� ���� ��������� ��������.
- -S
-
������ ���������� ������ readline, ����������� � ��������, � ���������� ���
�� ������� ������
- -f ���_�����
-
������ �������� ������ �� ���������� �����.
- -q �������
-
������ ���������� ������, ����������� � ��������� �������.
- -u �������
-
����� ��� �������� ���������� ������ � ��������� �������.
- -r keyseq
-
������� �������� � ��������� ���������� ������ keyseq.
������� ���������� �������� 0, ����� �������, ����� �������� �������������� ����� ���
��������� ������.
- break [n]
-
����� �� ����� for, while,
until ��� select.
���� �������� n �������, ����� �� n
������� ������. n ������ ���� ������ 1. ���� �������� n ��������� ����������
��������� ������, ���������� ����� �� ���� ������. ������� ����������
�������� 0, ���� ������ �� ��������� ��� �����.
- builtin ����������_������� [���������]
-
��������� ��������� ����������_������� ��������������, �������� ��
��������� � ���������� �� ������ ������. ��� ������� ��� ����������� �������,
��� ������� ��������� � ������ ���������� ������� �������������� ���, �����
����� ������������ ����� ���������� ������� � �������. ������ ���
���������������� ���������� ������� cd. ���� ��������� ������� �� ��������
���������� �������� ��������������, ������������ ������ 1.
- cd [-LP] [�������]
-
������ ��������� ������� �������. ����������� ������� �������� � ����������
HOME. ��������� CDPATH ���������� ���� ������ ���������, ����������
��������� �������.
�������������� ����� ��������� � �������� CDPATH ����������� ����������
(:). ������ ��� �������� � CDPATH ������������� �������� ��������, �.�. «.«.
���� ������� ���������� � ����� ����� (/), �� �������� ���������� CDPATH ��
������������. ����� -P ������� ������������ ���������� ��������� ���������
������ ���������� �� ���������� ������ (��. ����� ����� -P ����������
������� set);
����� -L ������� ��������� �� ���������� ������. �������� —
������������ $OLDPWD. ��� �������� ����� �������� �������� ������������
�������� 0, � ��������� ������ — 1.
- command [-pVv] ������� [�������� …]
-
��������� ������� � �����������, �� �������� ������� ����� �������
���������� ��������������. ����������� ������ ���������� ������� ���
�������, ������� ��������� � ��������� � ���������� PATH ���������. ����
������ ����� -p, ����� ������� ����������� �� ������������ ��������
���������� PATH, ��������������, ��� ����� ������� ��� ����������� �������.
���� ������� ����� -V ��� -v, �������� �������� �������. ����� -v ������ ����
����� — ��� ������� ��� ��� �����, ��������������� �������; ����� -V ����
������� ����� ��������� ��������. ���� ������� ����� -V ��� -v, ������ ������
����� 0, ���� ������� �������, � 1 � ��������� ������. ���� �� ���� �� ����
����� �� ������ � �������� ������ ��� ������� �� ������� �����, ����� �������
������ ������ 127. � ��������� ������ �������� ������ ���������� �������
command �������� ������ ������ ����������� �������.
- continue [n]
-
������� � ��������� �������� ����� for,
while, until ���
select. ���� �������
�������� n, ������� � ��������� �������� n-�� �������� �����. �������� n
������ ���� ������ 1. ���� n ��������� ���������� ��������� ������� ������,
����������� ��������� �������� ������ �������� ����� (����� «��������
������»). ������������ �������� 0, ���� ������ ������� �� ������� ��� �����
- declare [-afFirx] [-p] [���[=��������]]
typeset [-afFirx] [-p] [���[=��������]]��������� ���������� �/��� ������ �� ��������. ���� ����� �� ������, ��������
�������� ����������. ����� -p ����� �������� �������� � �������� ���������� �
���������� �������. ����� ������������ ����� -p, ��������� �����
������������. ����� -F ��������� ������ ����������� �������; ��������
������ ����� � �������� �������. ����� -F ������������� ������������� -f.
��������� ����� ����� ������������ ��� ������ ���������� ������ �
���������� ���������� ��� ��� ������� ��������� ����������:
-a
������ ��� — ���������� ���� ������ (��. ������ «�������» �����).
- -f
-
������������ ������ ����� �������.
- -i
-
���������� ��������� �������������; ��� ������������ ���������� ��������
����������� �������������� ��������� (��. ������
«���������� �������������� ���������«).
- -r
-
������ ��������������� ���������� ���������� ������ ��� ������. ����
���������� ������ ����������� �������� � ��������� ���������� � �� ������
����������.
- -x
-
�������� ����� ��� ��������������� � ����� ��� ����������� ������.
��� ������� ‘+‘ ������ ‘—‘ ������� ������������, �� ����������� ����, ��� �����
+a ������ ������������ ��� ����������� �������. ��� ������������� � �������,
������� ������ ������ ����� ���������, ��� � ������� local. ������������
�������� 0, ���� ������ �� ������� ���������������� �����, �� �������� �������
���������� ������� � ������� ����������� «-f foo=bar«, �� �������� �������
��������� �������� ����������, ��������� ������ ��� ������, �� ��������
������� ��������� �������� ������� ��� ������������� �����������
������������ ��� ������� (��. ������ «�������» �����),
�� ������������ ���
�������������� ���������� ���������� ��������������, �� �����������
������� �������� ������ ������ ��� ������ ����������, ��������� ��� ���������
������ ��� ������, �� �������� ������� �������� ������ ������� ��� ����������
���� ������, � �� ������������ ������ ����������� �������������� ������� �
������� ����� -f.
- dirs [-clpv] [+n] [-n]
-
��� ����� ��� ������� ������ ������ ������� ����������� ���������. ��
��������� �������� �������� � ���� ������ ����� ������. �������� �����������
� ������ � ������� ������� pushd;
������� popd ������� ������ �� ������.
+n
������ n-��� ������ ����� ������, ����������� �������� dirs ��� ������ ���
�����. ��������� ���������� � ����.
- -n
-
������ n-��� ������ ������ ������, ����������� �������� dirs ��� ������ ���
�����. ��������� ���������� � ����.
- -c
-
������� ���� ���������, ������ ��� ������.
- -l
-
������ ����� ������� �������; � ����������� ������� �������� ��� �����������
���������� �������� ������������ ������.
- -p
-
������ ���� ��������� �� ����� ������ � ������.
- -v
-
������ ���� ��������� �� ����� ������ � ������, ������� ������ ������ �����
������ �������.
���� �� ������� ������������ �����, � ������ n �� ������� �� ������� �����,
������������ �������� 0.
- disown [-ar] [-h] [������� …]
-
��� ������ ��� ����� ������ ��������� ������� ��������� �� ������� ��������
�������. ���� ������ ����� -h, ������� �� ��������� �� �������, � ���������� ���,
��� ������ SIGHUP �� ���������� ������� ��� ��������� ������� SIGHUP ���������
���������������. ���� ������� �� ������� � �� ������ ����� -a � -r,
������������ ������� �������. ���� ������� �� �������, ����� -a
�������� ������� ��� �������� ���
�������; ����� -r ��� ��������� ������� ������������ �������� ��������������
���������. ������� ���������� �������� 0, ���� ������ �� ������� ��������������
�������.
- echo [-neE] [�������� …]
-
������ ��������� ����� ������, �������� � ����� �������� ����� ������. ������
�������� — ������ 0. ���� ������� ����� -n, ������ ����� ������ �� ��������. ����
������� ����� -e, ���������� ������������� ��������� ����������� ����������
�������� � �������� �����. ����� -E ��������� ������������� ���� �����������
������������������� ���� � ��������, ��� ��� ���������������� �� ���������.
������� echo �� �������������� — ��� ������� ���������� �����. ������� echo
�������������� ��������� ����������� ������������������:
-
a
������ (������)
-
b
�����
-
c
�� �������� ����������� ������� ����� ������
-
\
����������� ������
-
f
������ ��������
-
n
����� ������
-
r
������� �������
-
t
�������������� ���������
-
v
������������ ���������
-
�������� �����
-
nnn
������, ASCII-��� �������� — ������������ �������� nnn (�� ����� �� ���� ����)
-
xnnn
������, ASCII-��� �������� — ����������������� �������� nnn (�� ����� �� ����
����)
- enable [-adnps] [-f ���_�����] [��� …]
-
�������� � ��������� ���������� ������� ��������������. ���������� ����������
������� ��������� ��������� ��� �������� ������� ����� ���� �� �����, ���
�������� ��������� �� ���������� ��������, ���� ������ ���������� �������
��������������� �������. ���� ������������ ����� -n, ��������� �� ������
������� �����������, � ��������� ������ ��� ���������. ��������, �����
������������ �������� ��������� test, ������� ��������� � ����� �� ��������� �
���������� PATH ���������, ������ ���������� ������ ��������������, ���������
enable -n test. ����� -f ������� ��������� ����� ���������� ������� � ���������
������ �� ��������� ������������ ���������� ����� � ��������, ��������������
������������ ��������. ����� -d ������� ���������� �������, ����� ����������� �
������� ����� -f. ���� ����� �� ������ ��� ���� ������� ����� -p,
�������� ������ ���������� ������ ��������������. ��� ���������� ������ ����� � ���������� ����
������ �������� ��� ���������� ���������� ������� ��������������. ���� ����� -n
�� �������, �������� ������ ����������� ���������� �������. ���� ������� �����
-a, ���������� ������ �������� ��� ���������� ������� � ��������� �������� ���
���������. ���� ������� ����� -s, �������� ������ ���������� �������,
�������������� ���������� POSIX. ������������ �������� 0, ���� ������ ���
������� ������ ���������� ������� �������������� � �� �������� �������� ���
�������� ����� ���������� ������� �� ����������� ���������� �����.
- eval [�������� …]
-
��������� �������� � ����������� � ������ �������. ��� ������� ����� �������� �
����������� ��������� ���������������, � �� ������ ������ ������������ �
�������� �������� ������� eval. ���� ���������� ��� ��� ��� ��������� ������,
������� eval ���������� 0.
- exec [-cl] [-a ���] [������� [���������]]
-
���� ������� �������, ��� �������� ��������� �������������. ��� ���� �����
������� �� ���������. ��������� ���������� ����������� �������. ���� �������
����� -l, ��������� ������������� �������� ����� � �������� �������� ���������,
������������� �������. ������ ��� ������ �������
login(1).
����� -c ��������
���������� ������� � ������ ������. ���� ������� ����� -a, ���������
������������� �������� ��� ��� ������� �������� ����������� �������. ����
������� �� ����� ���� ��������� �� ��� ��� ���� �������, ���������������
��������� ������������� ��������� ������, ���� ������ �� �������� �����
�������������� execfail, — � ���� ������ ������� ����������
��� ������. ������������� ��������� ������������� ���������� ��� ������ ���� ���� ��
����� ���� ��������. ���� ������� �� �������, ����� ��������������� �����������
� ������� ��������� �������������� � ������ ������ ����� 0. ���� ���
��������������� ��������� ������, ������ ������ ����� 1.
- exit [n]
-
�������� ���������� ������ ���������� �������������� �� �������� n. ����
�������� n �� �������, �������� ������ ����� ������ ������ ��������� �����������
�������. ����� ����������� ������ �������������� ���������� ���������� �������
EXIT.
- export [-fn] [���[=�����]] …
export -p��������� ����� ���������� ��� ��������������� ��������������� � �����
��������� ����������� ������. ���� ������� ����� -f, �������������� �����
�������. ���� ����� �� ������� ��� ������ ����� -p, �������� ������ ���� ����,
���������������� ������ ��������� ���������������. ����� -n �������� ��������
�������� ��������������� � ��������� ����������. ������� export ����������
������� ������ 0, ���� ������ �� ������� ������������ �����, ��� ��������������
���������� ��� � ����� -f �� ������� ���, �� ���������� ������ �������.
- fc [-e ��������] [-nlr] [������] [���������]
fc -s [pat=rep] [�������]��������� �������. ������ ����� �������� �������� ������, �� ������ ��
���������, �� ������ ������� ����������. ������ � ��������� ������� �����
�������� ������� (����� ��������� �������, ������������ � ���������������
������) ��� ������ (������ � ������ �������, ������ ������������� ��������
������������ ��� �������� �� ������ ������� �������). ���� ��������� �� �������,
��� ������ ��������� ��������� ������� ������� (��� ��� fc -l -10 ������ ���������
10 ������), � ��� ��������� ����� ��������������, ��� ��������� ��������� � ������.
���� ������ �� �������, �� ���������� ���������� ������� ��� �������������� � -16
(������������ � ����� ������ �������) ��� ������.
����� -n ��������� ������ ������� ������. ����� -r �������� ������� ������
������ �� ���������������. ���� ������� ����� -l, ������� �������� � �����������
�������� �����. � ��������� ������ ���������� ��������� ��������, ��������
���������� ���� � ���������������� ���������. ���� �������� �� �����,
������������ �������� ���������� FCEDIT ��� �������� ���������� EDITOR, ����
FCEDIT �� �����������. ���� �� ����������� ��� ����������, ������������ ��������
vi. �� ���������� ��������������, ����������������� ������� �������� �
�����������.
������ ����� ������� ���������� ���������� ������� ����� ������ �������
��������� pat �� rep. ������� ������ ��������� «r=fc -s«,
��� ��� ��� ������ «r cc» ����� ��������� ��������� �������, ������������
�� ������ «cc«, � ��� ����� «r» ����� �������� ����������� ��������� �������.
���� ������������ ������ �����, ������� ���������� �������� 0, ���� ������ ��
������� ������������ ����� ��� ������ ��� ��������� ������� ������� �� �������
������� ������ � ������ ������. ���� ������� ����� -e, ������������ ������ ������
��������� ����������� ������� ��� ������, ������������ ��� ������ � ���������
������ ������. ���� ������������ ������ �����, ������������ ������ ������
�������� ����������� �������, ���� ������ ������� �� ������ ��������������
������ � ������ �������, — � ���� ������ ������� fc ���������� 1.
- fg [�������]
-
������������ ������ ������� � ������������ ������ � ������ ��� �������
�������. ���� ������� �� �������, ������������ ������� ������� ���������� ��������������. ������������ �������� ������� ������ �������, ������������ � ������������
�����, ��� 1 ���� ���������� ��������� ��������� ���, ��� ���������� ����������
���������, ���� ������� �������������� ������� ��� �������, ���������� ���
����������� ���������� ���������.
- getopts ������_����� ��� [���������]
-
������� getopts ������������ ����������� ���������� �������������� ��� �������
����������� ����������. ������_����� �������� ����� �����, ������� ����������
����������; ���� ����� ����� ������� ���������, ��������������, ��� � ����� ������
���� ��������, ���������� �� ��� ���������� ��������. ��� ������ ������ �������
getopts �������� � ���������� �������������� � ��������� ������ ���������
�����, �������� ����������, ���� ��� ��� �� ����������, � ������ ����������
�������� — � ���������� OPTIND. OPTIND �������� �������� 1 ��� ������ ������
���������� �������������� ��� ��������. ���� ����� ������ ����� ��������,
������� getopts �������� ���� �������� � ���������� OPTARG. ���������
������������� �� ���������� �������� OPTIND �������������. ����� ��������
������� getopts � ��� �� ��������� ��������������, ���� ���������� ������������
����� ����� ����������, ���� ���������� ��� �������� ����.
����� ��� ����� ����������, ������� getopts ��������� ������ �� ���������,
������� ����. OPTIND ��� ���� �������� ������ ������� ���������, �� �����������
������, � ���������� ��� �������� �������� ?.
������� getopts ������ ������������ ����������� ���������, �� ���� ������
���������, getopts ��������� ��.
������� getopts ����� �������� �� ������� ����� ���������. ���� ������ ������
������_����� — ���������, ������������ �������������� �������������� ��
�������. ��� ������� ������ �������� ��������������� ���������, ����
�������������� ������������ ����� ��� ����������� ���������. ���� ����������
OPTERR ����� �������� 0, ��������� �� ������� �� ��������, ���� ���� ������
�������� ������_����� �� �������� ���������.
���� ���������� ������������ �����, getopts �������� ? � ����������
��� �, ���� �� ������ �������������� ��������������, ������ ��������� �� ������ �
���������� ���������� OPTARG. ���� getopts �������� � ������ ���������������
��������������, ������ ����� ���������� � ���������� OPTARG � �������
��������������� ��������� �� ��������.
���� ������������ �������� �� ���������, � ��� getopts �� ������ ��������������
��������������, ���������� ��� �������� �������� ?, ���������� OPTARG
������������, � �������� ��������������� ���������. ���� getopts �������� � ������
��������������� ��������������, ���������� ��� �������� �������� :, � �
���������� OPTARG ���������� ������ �����.
������� getopts ���������� 0, ���� �����, ��������� ��� �� ���������, ����������.
��� ���������� ����, ���� ��������� ����� ����� ��� ��������� ������.
- hash [-r] [-p ���_�����] [���]
-
��� ������� ����� ������������ � ������������ ������ ��� �������
����� ������ � ���������, ������������� � ���������� $PATH.
���� ������� ����� -p, ����� �� ��������� �� �����������, � ���������
��� ����� ������������ ��� ������ ��� �������. ����� -r ��������
����� ���� ����������� ����. ���� ��������� �� ������, ��������
���������� � ����������� ��������. ������ ������ — ������, ����
������ ��� ������� ����� � �� ������� ������������ �����.
- help [������]
-
������ �������� ���������� � ���������� ��������. ���� ������ ������,
������� help ������ ��������� ���������� ���������� � ���� ��������,
��������������� �������; � ��������� ������, �������� ������� �� ����
���������� �������� � ����������� ���������� ��������������. ������
������ — 0, ���� ������� ����� �������, ��������������� �������.
- history [-c] [n]
-
history -anrw [���_�����]
history -p �������� [�������� …]
history -s �������� [�������� …]���� ����� �� �������, ������ ������ ������� ������ � �������� �����.
������, ��� ������� ������� ��������� (*), ���� ��������.
��� �������� ��������� n �������� ������ ��������� n �����.
���� ������� ���_�����, ������ ������� ������� �� ����� �����; ���� ����
�� ������, ������������ �������� ���������� HISTFILE.
����� ����� ��������� ��������:
-a
��������� «�����» ������ ������� (������ �������, ��������� � ������
�������� ������ bash) � ���� �������.
- -n
-
������ ������ �������, ��� �� ����������� �� ����� �������, � ������� ������
������� ������. ���� ���� � �������, ����������� � ���� ������� � ������
�������� ������ bash.
- -r
-
������ ���������� ����� ������� � ������������ ��� � �������� �������� ������
������� ���������� ������.
- -w
-
���������� ������� ������ ������� ������ � ���� �������, ����������� ���
������� ����������.
- -c
-
�������� ������ ������� ���������� ������, ������ ��� ������.
- -p
-
��������� ����������� �� ������ ������� ��� ����������� ���������� � ������
��������� � ����������� �������� �����. ��������� �� ������������ �
������ �������. ��� ���������� ������� ����������� �� ������ �������,
���������� ����������� (����� � �������) ������ ��������.
- -s
-
��������� ��������� � ������ ������� ��� ���� ������. ��������� �������
� ������ ������� ��������� ����� ����������� ����������.
������������ �������� 0, ���� �� �������� ������������ �����
��� ��� ������� ������ ��� ������ ����� ������� �� ��������� ������.
- jobs [-lnprs] [ ������� … ]
jobs -x ������� [ ��������� … ]������ �� ������ �������� �������. ����� ����� ��������� ��������:
-l
������ ������� ���������� ������ �������������� ���������.
- -p
-
������ ������ �������������� ���������-������� ����� ���������, ����������
�������.
- -n
-
������ ���������� ������ � ��������, ��������� ������� ���������� � �������
���������� ����������� ������������ � ��������� �������.
- -r
-
������ ������ ������������� �������.
- -s
-
������ ������ ������������� �������.
���� ������� �������, �������� ���������� ������ �� ���� �������.
������ ������ — 0, ����� �������, ����� ������� ������������ ����� ��� �������������
��������������� �������.
���� ������� ����� -x, ������� jobs �������� ����� �������������
������� � ������� ��� ���������� ��������������� ��������������� ������
���������, � ��������� �������, ��������� �� ��������� � ��������� �� ������ ������.
- kill [-s ������ | -n �����_������� | -������] [�������������_�������� | �������] …
- kill -l [������ | ������_������]
-
�������� ��������� �� ����� ��� ������ ������ ��������� � ����������
���������������� ��� �������� � ������� � ��������� ���������������.
������ �������� ���� �� �����, ��������, SIGKILL, ���� �� ������.
���� ������ �������� �� �����, ��� ����� �������� ������� SIG, � �����
� �� ��������. ���� ������ �� ����� ����, �������������� ������
SIGTERM. ��� �������� ����� -l �������� ����� ��������.
���� ��� �������� ����� -l ������ ���������, �������� ������ �����
��������, ��������������� ����������, � ������������ ������ 0.
�������� ������_������ ����� -l — �����, �������� ���� ����� �������,
���� ������ ������ ��������, ����������� ���� ��������. ������� kill
���������� ������, ����, �� ������� ����, ���� ������ ��� ������
�������, ��� ����, ���� �������� ������ ��� ���� ������� ������������ �����.
- let �������� [�������� …]
-
������ �������� — �������������� ���������, ������� ����������
��������� (��. ������ «���������� �������������� ���������«).
���� ��������� �������� ����� �������� 0, ������� let ���������� 1,
� ��������� ������, ��� ���������� ������� 0.
- local [���[=��������] …]
-
��� ������� ��������� ��������� ��������� ���������� � ���������
������, ���������� ��������������� ��������. ����� ������� local
������������ � �������, ��� ������������ ������� �������� ����
���������� ��������������� �������� � ���������� �� ��� ���������.
��� ������ ��� ��������� ������� local ������ ������ ���������
���������� � ����������� �������� �����. ������������� �������
local ��� ������� — ������. ������ �������� — 0, ���� ������
������� local �� ������������ ��� ������� ��� �� ��������
������������ ���.
- logout
-
����� �� ���������� ���������� ��������������.
- popd [-n] [+n] [-n]
-
������� ������ �� ����� ���������. ��� ������ ��� ����������
������� ������� ������� �� ����� � ��������� ������� � �����
������� �������. ��������� ����� ��������� ��������:
+n
������� n-��� ������ ����� ������ ���������, ������� � ����.
��������: «popd +0» ������� ������ �������, � «popd +1» — ������.
- -n
-
������� n-��� ������ ������ ������ ���������, ������� � ����.
��������: «popd -0» ������� ��������� �������, �
«popd -1» — �������������.
- -n
-
��������� ������� �������� �������� �������� ��� �������� ���������
�� �����, ��� ��� ���������� ������ ���������� �����.
���� ������� popd ����������� �������, ����������� ����� �������
dirs � ������������ ������ 0. ������� popd ���������� ����,
���� �������� ������������ �����, ���� ��������� — ������, �������
�������������� ������ � ����� ��������� ��� �� ������� ������� �
��������������� �������.
- printf ������ [���������]
-
������ ��������� � ������������ � �������� � �����������
�������� �����. ������ — ������ ��������, ���������� ���
���� ��������: ������� �������, ������� ������ ����������
� ����������� �������� �����, ����������� ������������������
��������, ������������� � ���������� � ����������� �������� �����, �
������������ �������, ������ �� ������� �������� ������ ������������
���������. ������ ����������� �������� printf(1), ������������ %b
�������� ������ ����������� �������������������, ������������ �
�������� �����, � ������������ %q ������� ������ ���������������
�������� � �������, ������� ����� �������� ������������ � ��������
������� ������ ���������� ��������������.
������ ��� ������������� ������������ ��������, ����� ������� ���
���������. ���� ������ ������� ������ ����������, ��� ��������,
�������������� ������������ ������� ������������ ��� ���� �� ����
�������� ������� �������� ��� ������ ������, ��������������.
- pushd [-n] [�������]
pushd [-n] [+n] [-n]��������� ������� �� ������� ����� ��������� ��� ������������ ����, ����� �����
������� ������� ����� ������� ������� ���������. ��� ������ ��� ����������,
������ ������� ��� ������� �������� � ���������� 0, ����
���� ��������� �� ������. ��������� ����� ��������� ��������:
+n
������������ ���� ���, ��� n-��� ������� (��� �������� �����
�� ������ ���������, ������� � 0) ���������� ��������.
- -n
-
������������ ���� ���, ��� n-��� ������� (��� �������� ������
�� ������ ���������, ������� � 0) ���������� ��������.
- -n
-
��� ����� ��������� ������� ������� � ������� ��� ���������� ��������� �
����, ��� ��� �������� ����������� ������ �� ������.
- �������
-
��������� ������� �� ������� ����� ���������, ����� ��� �������
������� ���������.
���� ������� pushd ��������� �������, ����������� ����� �������
dirs. ����
������������ ������ �����, pushd ���������� 0 �� ����������� ������,
����� �� ������� ������� � �������. ��� ������������� ������ �����
pushd ���������� 0, ���� ������ ���� ��������� �� ������, �� ������ ��������������
������� ����� � ������� ������� � ����� ������� �������.
- pwd [-LP]
-
������ ������ ��� �������� ��������. ���������� ��� ����� �� �������� ����������
������, ���� ������� ����� -P ��� �������� ����� -o physical
���������� ������� set. ���� ������������ �����
-L, ����������� ������ �� ���������� ������. ������ ������ — 0, ���� ��
�������� ������ ��� ������ ����� �������� �������� ��� �� �������� ������������ �����.
- read [-er] [-a ���_�������] [-p �����������] [��� …]
-
������ ���� ������ �� ������������ �������� ������ � ����������� ������ �����
������� �����, ������ ����� — ������� �����, � ��� �����, ��� ��� ���������� �����
������ � ������������� ����� ���� ������������� � �������� �������� ����������
�����. ���� �� �������� ������ ��������� ������ ����, ��� ������� ����, ���������
����� �������� ������ ��������. ��� ��������� ������ �� ����� ������������ �������,
��������� � �������� ���������� IFS. ������ �������� ����� () ����� ������������
��� ����������� ������������� ���������� ������������ ������� � ��� ����������� �����
�� ��������� ������. ����� ����� ��������� ��������:
-r
�������� ����� �� �������������� ��� ������ ����������. ��� ��������� ������ ������.
� ���������, ��� ����������� �� ��������� ������ ������ ������������ ����
�������� �����/����� ������.
- -p
-
������ �����������, ��� ������������ �������� ������, ����� ������� ��������
������. ����������� �������� ������ ���� ������� ������ ���� � ���������.
- -a
-
����� ������������� ���������������� ��������� ������� ���_�������, ������� � 0.
���_������� ������������ ����� ������������� ����� ��������. ������ ����� � ���������� ������������.
- -e
-
���� ����������� ������� ����� ���� � ���������, ��� ��������� ������ ������������
���������� readline (��. ������ «���������� READLINE» �����).
���� ����� �� �������, ����������� ������ ������������� ���������� REPLY.
������ ������ — 0, ���� ������ �� ���������� ������ ����� �����.
- readonly [-apf] [��� …]
-
��������� ����� ���������� ��� ��������� ������ ��� ������; �������� ���������������
���������� ������ �������� � ���������� � ������� ������������. ���� ������� �����
-f, �������, ��������������� ������, ���� ����������. ����� -a ������������
�������� ������ ���������. ���� �� ���� ��� �� ������� ��� ������ ����� -p,
�������� ������ ����, ��������� ������ ��� ������. ����� -p �������� ������
����������� � �������, ������� ����� ������������ ��� �������. ������ ������ — 0,
���� ������ �� �������� ������������ �����, ���� �� ���� �� �������� ������
���������� ��� ����� -f �� ������� ����� ������, �� ���������� ������ �������.
- return [n]
-
�������� ���������� ������ ������� � ��������� �������� ������ n. ���� n �� �������,
������������ ������ ������ ��������� �������, ����������� � ���� �������. ��� ������������� ��� �������, �� � ���� ���������� �������� �������� . (source),
�������� ����������� ���������� �������� � ������� ���� �������� n, ����
������� ������ ��������� ������� ��������. ��� ������������� ��� ������� � �� ���
���������� �������� �������� ., ���������� 1.
- set [—abefhkmnptuvxBCHP] [-o �����] [�������� …]
-
��� ������ ��� ����� ������ ��� � �������� ���� ���������� ���������� �������������� � ����, ��������� ��� ���������� ���������� ������������. ��������� ������������ ��
������ � ������������ � ������� �������. ���� ����� �������, ��� ������������� ��� ���������� �������� ���������� ��������������. ��� ���������� ����� ��������� �����
��������� ������������� ��������������� � �������� �������� ����������� ����������
$1, $2,… $n. ����� ����� ��������� ��������:
-a
������������� �������� ���������� ��� ����������� ���������� ��� ��������������
� ����� ��� ����������� ������.
- -b
-
�������� � ��������� ���������� ������� ������� ����������, � �� ����� �������
���������� ���������� �����������. ��� ����� ��������� ������ ���� �������� ����������
���������.
- -e
-
���������� ��������� ������, ���� ������� �������
(��. ������ «��������� ������» �����)
��������� ������ � ��������� �������� ������. ������ ���������� �������������� ��
�����������, ���� ������������� �������� ������� �������� ������
����� until ��� while,
������ ��������� if, ������ ������ && ��� ||, ���
���� � ������� ������ ������� ����������� ��������� � ������� ��������� !.
- -f
-
��������� ����������� ���� ������.
- -h
-
���������� ��������������� ������, ��������� ��� ����������. ��� ����� �������� ��
���������.
- -k
-
�������� � ����� ��� ��������� � ���� ���������� ������������, � �� ������
�������������� ����� �������.
- -m
-
����� �����������. ���������� ��������� ��������. ��� ����� ���������� ���������������
��� ������������� ��������� ��������������� � ��������, ��� ���������� ���������
�������������� (��. ������
«���������� ���������» �����). ������� ��������
�������� � ��������� ������ ���������, � ������, ���������� �� ������ ������, ��������
��� ���������� �� ������.
- -n
-
������ �������, �� �� ��������� ��. ��� ����� ����� ������������ ��� ��������
������� �������������� ������ � ��������� ���������� ��������������. �������������
��������� �������������� �� ����������.
- -o ���_�����
-
���_����� ����� ����� ���� �� ��������� ��������:
allexport
�� ��, ��� � ����� -a
- braceexpand
-
�� ��, ��� � ����� -B
- emacs
-
������������ ��������� �������������� ��������� ������ � ����� ��������� emacs.
��� ����� ����������� �� ��������� � ������������� ��������� ���������������, ����
������ ��� �� �������� � ������ —noediting
- errexit
-
�� ��, ��� � ����� -e
- hashall
-
�� ��, ��� � ����� -h
- histexpand
-
�� ��, ��� � ����� -H
- history
-
�������� ��������� ������� ���������� ������, ��� ������� �����
� ������� «������� ���������� ������«.
��� ����� ����������� �� ��������� ��� ������������� ��������� ���������������.
- ignoreeof
-
��������� ����� ��, ��� � ����� ���������� ������� IGNOREEOF=10 (��. ������
«���������� ���������� ��������������» �����)
- keyword
-
�� ��, ��� � ����� -k
- monitor
-
�� ��, ��� � ����� -m
- noclobber
-
�� ��, ��� � ����� -C
- noexec
-
�� ��, ��� � ����� -n
- noglob
-
�� ��, ��� � ����� -f
- notify
-
�� ��, ��� � ����� -b
- nounset
-
�� ��, ��� � ����� -u
- onecmd
-
�� ��, ��� � ����� -t
- physical
-
�� ��, ��� � ����� -P
- posix
-
�������� ��������� bash �� ��������������� ���������
POSIX 1003.2, ���, ��� ��� ����������
- privileged
-
�� ��, ��� � ����� -p
- verbose
-
�� ��, ��� � ����� -v
- vi
-
������������ ��������� �������������� ��������� ������ � ����� ��������� vi.
- xtrace
-
�� ��, ��� � ����� -x.
���� ������� ����� -o ��� �����_�����, �������� �������� ������� ������������� �����.
���� ������� ����� +o ��� �����_�����, � ����������� �������� ����� �������� �����
������ set, ����������� ��� ����������� �������� ������ �����.
- -p
-
�������� ����������������� �����. � ���� ������ ����� $ENV � $BASH_ENV ��
��������������, ������� ���������� �������������� �� ����������� �� �����, � ����������
����� SHELLOPTS ������������. ���� ��������� ������������� ������� � �����������
��������������� ������������ (������), �� ����������� � ��������, � ����� -p ��
������, ����������� ������������� �������� � ����������� �������������
��������������� ������ ���������. ���� ����� -p ������ ��� �������, �����������
������������� �� ������������. ���������� ������ ����� �������� � ��������� ����������� ��������������� ������������ � ������ ������� ��������.
- -t
-
��������� ����� ������ � ���������� ����� �������.
- -u
-
��� ����������� �������� ���������� ������������� �� ������������� ���������� ���
������. ��� ������� ����������� �������� �� ������������ ���������� ���������
������������� ������ ��������� �� ������ �, ���� �� — �� �������������, ���������
������ � ��������� �������� ������.
- -v
-
�������� ������ ������ �� ���� ������.
- -x
-
����� ����������� � ������ ������� ������� �������� �������� ���������� PS4, � ����� —
������� � ������������ ����������� � ����������.
- -B
-
��������� ����������� ���������� ������ � ��������� �������������� (��. ������
«����������� ��������� � �������» �����).
��� ����� ����������� �� ���������.
- -C
-
���� ��� ����� �����������, bash �� ������������ ������������ ���� ��� ����������������
� ������� ���������� >, >& � <>. ��� ��������� �����
�������������� ��� �������� �������� ������, �������� �������� ���������������
|> ������ >.
- -H
-
�������� ����������� �� ������ ������� � ������� ����������� !. ��� �����
�� ��������� ����������� � ������������� ��������� ���������������.
- -P
-
���� ��� ����� �����������, ��������� ������������� �� ������� �� ����������
������ ��� ���������� ������ ���� cd, ���������� ������� ������� �������. ������ �����,
�� ���������� ���������� ��������� ���������. �� ���������, bash ������� ��
���������� ������� ��������� ��� ���������� ������, ���������� ������� �������.
- —
-
���� ����� ���� ����� ��� ����������, ������������ �������� ����������� ����������.
� ��������� ������, ����������� ��������� ��������������� � ������������ �
�����������, ���� ���� ��������� �� ��� ���������� � ������ (—).
- —
-
������������� �� ��������� �����, ��� ��� ��� ���������� ��������� �������������
����������� ����������. ����� -x � -v ��� ���� �����������. ���� ������
���������� ���, ����������� ��������� �������� ��� ���������.
����� �� ��������� ���������, ���� ���� �� ������� �����. ��� �������� + ������ —
����� ������ �����, ����� �����������. ����� ����� ����� �������� ��� ��������� ���
������ ���������� ��������������. ������� ����� ����� ����������� � ���� ��������
��������� $-. ������ ������ — 0, ���� ������ �� ������� ������������ �����.
- shift [n]
-
����������� ��������� n+1 … ����������������� � $1 …. ��������� �� $#-n+1 ��
$# ������������. n ������ ���� ��������������� ����� ������, �� ����������� $#.
���� n ����� 0, ��������� �� ����������. ���� n �� �������, ��������������
�������� 1. ���� n ������ $#, ������������ ��������� �� ����������. ������ ������ —
������ ����, ���� n ������ $# ��� ������������� � 0 � ��������� ������.
- shopt [-pqsu] [-o] [���_����� …]
-
����������� �������� ����������, ����������� �� ������������ ���������� ����������
��������������. ���� ����� �� ������� ��� ������ ����� -p, �������� ������ ����
��������� �����, � ���������, ����������� ����� ��� ���. ����� -p ��������
������ ����������� � �������, ����������� ������������ �� � �������� ������� ������.
��������� ����� ����� ��������� ��������:
-s
�������� (�������������) ��� ��������� �����.
- -u
-
��������� (����������) ��� ��������� �����.
- -q
-
��������� ������ ����������� ���������� (�������������� �����); ������ ������
����������, ����������� ����� ��� ���. ���� ����� -q ������� ��������� ����_�����,
������ ������ — 0, ���� ��� ��������� ����� �������� � �� 0 � ��������� ������.
- -o
-
������������ �������� ����_����� ������ ��������������� �
����� -o ���������� ������� set.
���� ����� -s ��� -u ������������ ��� ����������, �������� ���������� ������ ��
������������� ��� ��������������� ������, ��������������. ���� �� ������� �����, �����
������� shopt �� ��������� ��������� (��������).
������ ������ ��� ������ ����� — 0, ���� ��� ��������� ����� �������� � �� 0 �
��������� ������. ��� ��������� ��� ������ ����� ������ ������ — 0, ���� ������ ��
������� ������������ ����� ���������� ��������������.
��� ������ ���������� ����� ������� shopt:
- cdable_vars
-
���� ��� ����� ����������� � ���������� ���������� ������� cd
�������� �� �������� ������ ��������, �� ��������������, ��� ��� — ��� ����������,
�������� ������� — ������� ��� ��������.
- cdspell
-
���� ��� ����� �����������, ��������� ������ � ��������� ���� ��������� ���
������� cd ����� ������������. ���� ���� � �������������� ������� ��������,
����������� ������� � ����� ������ �������. ���� ��� ����������� ������ ����������
�������, �������� ������������ ��� �������� � ������� �����������. ��� �����
������������ ������ �������������� ���������� ����������������.
- checkhash
-
���� ��� ����� �����������, bash ���������, ��� ��������� � ���� �������
����������, ������ ��� �������� �� ���������. ���� ����������� � ���� ������� ������ ��
����������, ����������� ������� ����� �� ���������.
- checkwinsize
-
���� ��� ����� �����������, bash ��������� ������ ���� ����� ���������� ������
������� �, ��� �������������, ��������� �������� ���������� LINES � COLUMNS.
- cmdhist
-
���� ��� ����� �����������, bash �������� ��������� ��� ������ �������������
������� � ����� ������ ������ �������. ��� ��������� ����� ������������� �������������
�������.
- dotglob
-
���� ��� ����� �����������, bash �������� � ���������� ����������� ���� ������
�����, ������������ � ����� (.).
- execfail
-
���� ��� ����� �����������, �� ������������� ��������� ������������� �� �����
��������� ������, ���� �� ������ ��������� ����, ��������� � �������� ��������� ���
���������� ������� exec.
������������� ��������� ������������� �� ��������� ������,
���� ������� exec �� �����������.
- expand_aliases
-
���� ��� ����� �����������, ���������� ���������� ���, ��� ���� ������� ����� �
������� «����������«. ��� ����� �������� �� ���������
� ������������� ��������� ���������������.
- extglob
-
���� ��� ����� �����������, �������������� ����������� ����������� ������������� � ��������, ��������� ����� � �������
«����������� ���� ������«.
- histappend
-
���� ��� ����� �����������, ������ ������� ����������� � ����, ��� �������� ��������
��������� ��������� HISTFILE, ��� ���������� ������ ���������� ��������������, � ��
������������ ����.
- histreedit
-
���� ��� ����� ����������� � ������������ ���������� readline, ������������ ��������
����������� �������� ������������� ������� ��� ������������� ����������� �� ������
�������.
- histverify
-
���� ��� ����� ����������� � ������������ ���������� readline, ���������� �����������
�� ������ ������� �� ���������� ����������� ���������� �������������� ����������.
������ �����, ���������� � ���������� ������ ����������� � ����� ��������������
readline ��� ���������� ���������.
- hostcomplete
-
���� ��� ����� ����������� � ������������ ���������� readline, bash ����� ��������
��������� ����������� ���� ������ ����� ���������� �����, ����������� ������ @, (��.
��������� «����������» � �������
«���������� READLINE» �����).
��� ����� �������� �� ���������.
- huponexit
-
���� ��� ����� �����������, bash ����� �������� ������ SIGHUP ���� ��������
��� ���������� ������ �������������� ���������� ���������� ��������������.
- interactive_comments
-
���� ��� ����� �����������, �����, ������������ �������� #, � ��� ��������� ������� �
���� ������ ������������ ������������� ��������� ��������������� (��. ������
«�����������» �����). ��� ����� �������� �� ���������.
- lithist
-
���� ��� ����� �����������, � ����������� ����� cmdhist,
������������� �������, �� �����������, ����������� � ������ ������� �� �����������
��������� ����� ������ ������ ����� � ��������.
- mailwarn
-
���� ��� ����� ����������� � ����, ����������� ��������� ��������������� bash ���
�������� ����, ������� � ������� ��������� ��������, �������� ���������
«The mail in ���_����� has been read«.
- nocaseglob
-
���� ��� ����� �����������, bash ��� ����������� ���� ������ ��������� �������������
� �������� ��� ����� �������� �������� (��. ������
«����������� ���� ������» �����).
- nullglob
-
���� ��� ����� �����������, bash ����������� ������ ������ ������ ��������, �������
�� ������������� �� ���� ���� (��. ������
«����������� ���� ������» �����), � ��
��� ������.
- promptvars
-
���� ��� ����� �����������, � ������� ����������� ����������� ����������� ��������
���������� � ���������� ����� ����������� �����������, ��������� � �������
«�����������» �����. ��� ����� ����������� �� ���������.
- restricted_shell
-
��������� ������������� ������������� ��� �����, ���� �� ������� � ������������
������ (��. ������ »
������������ ��������� �������������» ����). �������� ����
����� �������� ������. ��� �� ������������ ��� ���������� ������ ���������� �������,
��� ��������� � ���� ������ ������, �������� �� ��������� ������������� ������������.
- shift_verbose
-
���� ��� ����� �����������, ���������� ������� shift ������ ��������� ��
������, ����� �������� ������ ��������� ���������� ����������� ����������.
- sourcepath
-
���� ��� ����� �����������, ���������� ������� source
(.) ���������� �������� ���������� PATH ��� ������ ��������, ����������� ����,
���������� � �������� ���������. ��� ����� �� ��������� ��������.
- suspend [-f]
-
���������������� ������ �������� ���������� ��������������, ���� �� �� �������
������ SIGCONT. ����� -f ��������� ��������������, ���� ��� ������� ����������� �
��������� ��������� �������������� — ���������� ������������� ������. ������ ������ —
0, ���� ������ ��������� ������������� �� �������� ���������, � ����� -f — �� �������,
��� ���� ���������� ��������� ���������.
- test ���������
[ ��������� ]���������� ������ 0 ��� 1, � ����������� �� ����������� �������� ��������� ���������.
������ �������� ��� ������� ������ ���� ��������� ����������. ��������� ������� ��
���������, ��������� ����� � ������� «�������� ���������«.
��������� ����� ������������� � ������� ��������� ����������, ������������� ��
�������� ����������.
- ! ���������
-
�������, ���� ��������� �����.
- ( ��������� )
-
���������� �������� ���������. ����� ������������ ��� ��������������� ��������
���������� ���������� ����������.
- ���������1 -a ���������2
-
�������, ���� ������� ��� ���������.
- ���������1 -o ���������2
-
�������, ���� ������� ���������1 ��� ���������2.
������� test � [ ��������� �������� ��������� � ������� ������ ������, �����������
�� ���������� ����������.
- 0 ����������
-
��������� �����.
- 1 ��������
-
��������� ������� ������ ���� �������� — �� ������.
- 2 ���������
-
���� ������ �������� — !, ��������� ������� ������ ���� ������ �������� —
������. ���� ������ �������� — ���� �� ������� �������� ����������, ������������� �����
� ������� «�������� ���������«, ��������� �������,
���� ������� �������� ���������� ������. ���� ������ �������� �� ��������
���������� ������� �������� ����������, ��������� �����.
- 3 ���������
-
���� ������ �������� — ���� �� �������� �������� ����������, �������������
����� � ������� «�������� ���������«,
����������� �������� ����� �������� ���������
��������� ���������, ������������� ������ � ������ ��������� � �������� ���������. ����
������ �������� — !, ��������� — ��������� ��������������� �������� ������� � ��������
����������. ���� ������ �������� — ������ (, � ������ — ������ ),
����������� ����� ��������� ��������������� �������� ������� ���������. � ��������� ������,
��������� �����. � ���� ��������� ��������� -a � -o ��������� ���������.
- 4 ���������
-
���� ������ �������� — !, ��������� — ��������� ��������������� �������� ����������
����������. � ��������� ������, ��������� ����������� � ����������� � ������������
� ������������ �� ������������� ���� ��������.
- 5 � ����� ����������
-
��������� ����������� � ����������� � ������������ � ������������ �� ������������� ���� ��������.
- times
-
������ ����������� ���������������� � ��������� ����� ������ ��� ����������
�������������� � ���������� �� ���� ���������. ������ ������ — 0.
- trap [-lp] [��������] [������ …]
-
������� �������� �������� � ����������� ��� ��������� ��������� ���������������
��������� ��������. ���� �������� �� ������ ��� ����� ��� —, ��� ���� ���������
�������� ��������������� �������� ����������� (��, ��� ���� ��� �������
���������� ��������������). ���� �������� — ������ ������, ��� ��������� �������
������������ ��������� ��������������� � ����������� �� ���������. ���� �������
����� -p, �������� ������� trap, ��������� � ������ �� ������������� ��������.
���� ���������� ��� ��� ������� ������ ����� -p, ������� trap ������ ������
������ (������������), ��������� � ������ �� ��������.
������ ����� �������� ��� �� �����,
������������� � ����� <signal.h>, ��� � �� ������. ���� � �������� �������
������� ����������� EXIT (0), �������-�������� ����������� ��� ���������� ������
���������� ��������������. ���� � �������� ������� ������� DEBUG,
�������-�������� ����������� ����� ������ ������� �������
(��. ������ «��������� ������» �����).
����� -l �������� � ������ ������ ���� �������� � ��������������� �� �������.
�������, ���������������� ��� ����� � ��������� �������������, ������ ����������� ���
��������������. ������������� ������� ������������ � �������� �������� � �����������
��������� ��� ��������. ������ �������� — 1, ���� ������ ������������ ������; �
��������� ������, trap ���������� 0.
- type [-atp] ��� [��� …]
-
��� ������ ��� ����� ����������, ��� ����� ����� ������������������
��� ������������� � �������� ����� �������. ���� ������� ����� -t, type ������
���� �� ����� alias, keyword, function, builtin ��� file,
���� ���, ��������������,
������������ ����� ���������, ����������������� ����� ���������� ��������������,
�������, ���������� ������� ��� ���� �� �����. ���� ��� �� �������, ������ ��
�������� � ������������ ������ ������ 1. ���� ������� ����� -p, type ������
���� ��� ����� �� �����, ������� ����� ����������� ��� �������� ����� � ��������
����� �������, ���� ������, ���� ��������� � ����� �� �����. ���� ������� �������� �
����, ����� -p �������� � ������ �������� �� ����, � �� ����������� ������� �����,
����������� �� ������������� � PATH ���������. ���� ������������ ����� -a, �������
type ������ ������ ���� ���������, ��� ���� ����������� ���� � ��������������� ������. � ������ ���������� ����� ���������� � �������, ���� ������ �� ������� ����� -p. �
���� ������ �� ����������, ���� ������� ����� -a. ������� type ���������� 0,
���� ���� ���� �� ���������� ������, � 1 � ��������� ������.
- ulimit [-SHacdflmnpstuv [�����������]]
-
������������ ���������� ���������, ���������� ��� ���������� �������������� �
���������� �� ��������� � ��������, �������������� ����� ����������. ��������
����������� ����� ���� ������ ������ ��������� ������� ��� ������� unlimited.
����� -H � -S ���������, ��� ��� ������� ������� ��������������� ������� ��� ������
�����������. ������� ����������� ����� ��������� ������������ ������; ������
����������� ����� ������������ ������ �� �������� ���������������� �������������������. ���� ����� -H � -S �� �������, ��������������� � ������, � �������
�����������. ���� ����������� �� �������, �������� ������� �������� ������� �����������
�� ������, ���� ������ �� ������� ����� -H. ���� ������� ��������� ��������,
����� ��������� �������� �������� � ������� ��������� �������. ������ �����
���������������� ��������� �������:
-a
�������� ���������� � ���� ������� ������������
- -c
-
������������ ������ ����������� ������ core
-d
������������ ������ �������� ������ ��������
-f
������������ ������ ������, ����������� ��������� ���������������
-l
������������ ������ ��������, ������� ����� ���� �������� � ������
-m
������������ ������ ������������ ������
-n
������������ ���������� �������� �������� ������������ (����������� ������ ��
��������� ������������� ��� ��������)
-p
������ ������������ ������ � 512-�������� ������ (��� ������������� ������)
-s
������������ ������ �����
-t
������������ ���������� ������������� ������� � ��������
-u
������������ ���������� ��������� ��� ������ ������������
-v
������������ ����� ����������� ������, ��������� ���������� ��������������
���� ����������� �������, ��� ���������� ����� ���������� ��������� ���
���������������� ������� (����� -a ������ ������ ����������). ���� ����� ��
������, �������������� ����� -f. �������� �������������� � �������� 1024 �����, ��
����������� ����� -t, ��� �������� — � ��������, -p, ��� �������� — � 512-��������
������, � ����� -n � -u, ��� ������� �������� �� ��������������. ������ ������ — 0,
���� �� ������� ������������ �����, � �������� ����������� �� ������ �� �������� ��������, �������� �� unlimited, � ��� ��������� ������ ����������� �� ��������� ������.
- umask [-p] [-S] [�����]
-
���������������� ����� �������� ����� ��������������� ������ ��������� �����. ����
����� ���������� � �����, ��� ���������������� ��� ������������ �����; ����� — ���
����� � ���������� �������, ����������� ������������� � ������� chmod(1). ����
����� �� ������� ��� ������ ����� -S, �������� ������� �������� �����. ����� -S
�������� ������ ����� � ���������� �������; �� ��������� �������� ������������
�����. ���� ������� ����� -p, � ����� �� ������, ��������� �������� � ����,
������� ����� ������������ �� ������� �������. ������ ������ — 0, ���� ����� ����
������� �������� ��� �� �������, � 1 � ��������� ������.
- unalias [-a] [��� …]
-
������� ����� �� ������ ������������ �����������. ���� ������� ����� -a, ���������
��� �����������. ������������ �������� 0, �� ����������� �������, ����� ����������
��� �� �������� �����������.
- unset [-fv] [��� …]
-
��� ������� ����� ������� (����������) ��������������� ���������� ��� �������.
���� ����� �� ������� ��� ������ ����� -v, ��������� ������ ����������.
���������� ������ ��� ������ ������� ������. ���� ������� ����� -f,
��������� ������ ����������� ��������������� �������. ��������� ����������
��� ������� ��������� �� �����, ������������ ����������� ��������. ����
������������ ���� �� ���������� RANDOM, SECONDS, LINENO,
HISTCMD ��� DIRSTACK,
��� ���������� ������ ����������� ��������, ���� ���� ��� � ����������
����� ���������������. ������ ������ — 0, �� ���������� �������, ����� ��� ��
���������� ��� �������� ������ ��� ������.
- wait [n]
-
���� ���������� ���������� �������� � ���������� ������ ��� ����������. n �����
���� ��������������� �������� ��� ������������� �������; ���� ������� ������������
�������, ���� ���������� ���� ��������� � ��������� ���������������� �������. ����
n �� �������, ��������� ���������� ���� ������� �������� ����������� ���������, �
������������ ������ ����. ���� n ������ ������������� ��������������� �������� ���
�������, ������ ������ — 127. � ��������� ������, �������� ������ ��������
������ ���������� ���������� �������� ��� �������, ���������� �������� �����.
������������ ��������� �������������
���� bash ������� �� ����� rbash ��� � ������ -r ��� ������,
��������� ������������� ���������� ������������. ������������ ���������
������������� ������������ ��� �������� ����� ����������� ������ �� ��������� �
������� ���������������. �� ����� ���� ���������� bash, �� ��������� ��������
�� ��������� ��� �� �����������:
- *
- ��������� �������� �������� � ������� cd
- *
-
��������� ��� ����� �������� ���������� SHELL, PATH, ENV ���
BASH_ENV
- *
- �������� / � ������ ������
- *
- �������� ����� �����, ����������� /, � �������� ��������� ��� ���������� ������� .
- *
- �������������� ����������� ������� �� ����� ���������� �������������� ��� �������
- *
-
������ �������� ���������� SHELLOPTS �� ����� ���������� �������������� ���
�������
- *
-
��������������� ������ � ������� ���������� >, >|, <>, >&, &>
� >>
- *
-
������������� ���������� ������� exec
��� ������ ���������� �������������� ������ ��������
- *
-
���������� ��� �������� ���������� ������ � ������� ����� -f � -d
���������� ������� enable
- *
- �������� ����� -p ��� ���������� ������� command
- *
-
���������� ������ ������������� ���������� �������������� � ������� �����
set +r ��� set +o restricted.
��� ����������� ����������� ����� ������ ������ ���������� �������.
��� ���������� �������, ������� ��������� ��������� ���������� ��������������
(��. ������ «���������� ������» �����),
rbash ��������� ����� ����������� �
��������� ��������������, ����������� ��� ���������� ��������.
������
-
Bash Features, Brian Fox and Chet Ramey
The Gnu Readline Library, Brian Fox and Chet Ramey
The Gnu History Library, Brian Fox and Chet Ramey
Utilities, IEEE Portable Operating System Interface (POSIX) Part 2: Shell
�
sh(1),
ksh(1),
csh(1),
vi(1)
�����
- /bin/bash
-
����������� ���� bash
- /etc/profile
-
������������� ���� �������������, ����������� ���������� ���������� ����������������
- ~/.bash_profile
-
������ ���� �������������, ����������� ���������� ���������� ����������������
- ~/.bashrc
-
��������� ���� ���������� ������� ��� ������������� ��������� ���������������
- ~/.bash_logout
-
��������� ���� ������� ��� ���������� ���������� ��������������, ����������� ���
���������� ��� ������
- ~/.inputrc
-
��������� ���� ������������� ���������� readline
������
������ ���� (Brian Fox), Free Software Foundation
<bfox@gnu.ai.MIT.Edu>
��� ���� (Chet Ramey), Case Western Reserve University
<chet@ins.CWRU.Edu>
���������� �� �������
���� �� ���������� ������ � bash, �������� � ���. �� ������� ���������, ���
��� ������������� ������, � ��� ��� ����������� � ��������� ��������� � ��� ������
bash.
����� ����, ��� ����������, ��� ������ ������������� ����������, ����������� ������� bashbug ��� ������� ��������� �� ������. ���� �� ������, ��� ������ ���������,�������� � �����������! ����������� � «�����������» ��������� �� �������
������ ���������� �� ����������� ����� �� ������
<bug-bash@gnu.org> ��� � �������������
������ Usenet gnu.bash.bug.
��� ��������� �� ������� ������ ��������:
- *
- ����� ������ bash
- *
- ���������� �� ���������� ��������� � ������������ �������
- *
- ����������, ���������������� ��� ����������
- *
- �������� ���������� ���������
- *
- �������� �������� ��� «������», ����������� ������������� ��������
������� bashbug ��������� ������ ��� ���������� � ������������ ������ ���������
�� ������ �������������.
����������� � ��������� �� ������� �� ���� �������� ����������� �����������
����������� �� ������ <chet@ins.CWRU.Edu>.
����������� � ��������� �� ������� � �������� ���� �������� ����������� �����������
�� ������� ���� ����������� �� ������ <valera@openxs.kiev.ua>.
������
������� ������� � ������� ���������.
���� ��� ��������� ������� ����� bash � ������������� �������� sh, � ��������,
��������� � ���������� ������������ POSIX.
���������� ������ ����� ������� � �����.
���������� ������� � ������� �������������� ������ ����������/�������������.
��������� ������� � ������������������ ������ ���� ‘a ; b ; c’ ��������������
����������� ��� ������� ������������ ��������. ����� ������� ���������������,
��������� ������������� ���������� ��������� ��������� ������� � ������������������.
��� ���������� ������������������ ������ � ����������� ��������� ��������������, ���
��������� ������������� ��, ��� ������ �����, ���������� ����� �� � ������.
������� � ��������� ����������� ����������� ���������� ������� $(…) ��
�����������, ���� �� ����� ��������� ������� �����������. ��� �����������
������ ��������� �� ������ �� ������������ ����� ��� ����� �������.
������ (����) �������������� �������.
����������
�������� ��� bash �������� � ������ SUNWbashS.
Index
- ��������
- ���������
- ��������� �����
- ��������
- �����
- ���������
- �����
- �����������
- ����������������� �����
- ��������� ������
-
- ������� �������
- ���������
- ������
- ��������� �������
- �����������
- ����������
- ���������
-
- ����������� ���������
- ����������� ���������
- ���������� ���������� ��������������
- �������
- ������
-
- ������ ��������� � �������� �������
- ������ ������
- ����������� �������� ����������
- ����������� ����������� ���������� ������
- ����������� ���������
- ��������� �� �����
- ����������� ���� ������
- �������� �������
- ���������������
-
- ��������������� �������� ������
- ��������������� ��������� ������
- ���������� ����������������� ������
- ��������������� ������������ ��������� ������ � ������������ ������ ������
- ����������� �������� �����
- ������������ ������������ ������
- �������� ������������ ������ ��� ������ � ������
- ����������
- �������
- ���������� �������������� ���������
- �������� ���������
- ��������� ������� �������
- ���������� �������
- ����� ���������� ������
- �����
- ������ ������
- �������
- ���������� ���������
- �����������
- ���������� READLINE
-
- ���������� �� ������ ������ readline
- ������������� ���������� readline
- ������� ������� readline
- ���������� readline
- ����������� ����������� readline
- �����
- ������� ��� �����������
- ������� ��� ������ �� ������� �������
- �������� � �������
- �������� ���������
- ����������
- ������������ �������
- ������ �������
- ������� ���������� ������
- ����������� ������ �� ������ �������
-
- ������� �������
- ������� ����
- ������������
- ���������� ������� ��������������
- ������������ ��������� �������������
- ������
- �����
- ������
- ���������� �� �������
- ������
- ����������
bash-handbook
This document is written for those who want to learn Bash without diving in too deeply.
Tip: Try learnyoubash — an interactive workshopper based on this handbook!
Node Packaged Manuscript
You can install this handbook using npm. Just run:
$ npm install -g bash-handbook
You should be able to run bash-handbook at the command line now. This will open the manual in your selected $PAGER. Otherwise, you may continue reading on here.
The source is available here: https://github.com/denysdovhan/bash-handbook
Translations
Currently, there are these translations of bash-handbook:
- Português (Brasil)
- 简体中文 (中国)
- 繁體中文(台灣)
- 한국어 (한국)
Request another translation
Table of Contents
- Introduction
- Shells and modes
- Interactive mode
- Non-interactive mode
- Exit codes
- Comments
- Variables
- Local variables
- Environment variables
- Positional parameters
- Shell expansions
- Brace expansion
- Command substitution
- Arithmetic expansion
- Double and single quotes
- Arrays
- Array declaration
- Array expansion
- Array slice
- Adding elements into an array
- Deleting elements from an array
- Streams, pipes and lists
- Streams
- Pipes
- Lists of commands
- Conditional statements
- Primary and combining expressions
- Using an
ifstatement - Using a
casestatement
- Loops
forloopwhileloopuntilloopselectloop- Loop control
- Functions
- Debugging
- Reading User Input
- Afterword
- Want to learn more?
- Other resources
- License
Introduction
If you are a developer, then you know the value of time. Optimizing your work process is one of the most important aspects of the job.
In that path towards efficiency and productivity, we are often posed with actions that must be repeated over and over again, like:
- taking a screenshot and uploading it to a server
- processing text that may come in many shapes and forms
- converting files between different formats
- parsing a program’s output
Enter Bash, our savior.
Bash is a Unix shell written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. It was released in 1989 and has been distributed as the Linux and macOS default shell for a long time.
So why do we need to learn something that was written more than 30 years ago? The answer is simple: this something is today one of the most powerful and portable tools for writing efficient scripts for all Unix-based systems. And that’s why you should learn bash. Period.
In this handbook, I’m going to describe the most important concepts in bash with examples. I hope this compendium will be helpful to you.
Shells and modes
The user bash shell can work in two modes — interactive and non-interactive.
Interactive mode
If you are working on Ubuntu, you have seven virtual terminals available to you.
The desktop environment takes place in the seventh virtual terminal, so you can return to a friendly GUI
using the Ctrl-Alt-F7 keybinding.
You can open the shell using the Ctrl-Alt-F1 keybinding. After that, the familiar GUI will disappear and one of the virtual terminals will be shown.
If you see something like this, then you are working in interactive mode:
Here you can enter a variety of Unix commands, such as ls, grep, cd, mkdir, rm and see the result of their execution.
We call this shell interactive because it interacts directly with the user.
Using a virtual terminal is not really convenient. For example, if you want to edit a document and execute another command at the same time, you are better off using virtual terminal emulators like:
- GNOME Terminal
- Terminator
- iTerm2
- ConEmu
Non-interactive mode
In non-interactive mode, the shell reads commands from a file or a pipe and executes them. When the interpreter reaches the end of the file, the shell process terminates the session and returns to the parent process.
Use the following commands for running the shell in non-interactive mode:
sh /path/to/script.sh
bash /path/to/script.sh
In the example above, script.sh is just a regular text file that consists of commands the shell interpreter can evaluate and sh or bash is the shell’s interpreter program. You can create script.sh using your preferred text editor (e.g. vim, nano, Sublime Text, Atom, etc).
You can also simplify invoking the script by making it an executable file using the chmod command:
chmod +x /path/to/script.sh
Additionally, the first line in the script must indicate which program it should use to run the file, like so:
#!/bin/bash echo "Hello, world!"
Or if you prefer to use sh instead of bash, change #!/bin/bash to #!/bin/sh. This #! character sequence is known as the shebang. Now you can run the script like this:
A handy trick we used above is using echo to print text to the terminal screen.
Another way to use the shebang line is as follows:
#!/usr/bin/env bash echo "Hello, world!"
The advantage of this shebang line is it will search for the program (in this case bash) based on the PATH environment variable. This is often preferred over the first method shown above, as the location of a program on a filesystem cannot always be assumed. This is also useful if the PATH variable on a system has been configured to point to an alternate version of the program. For instance, one might install a newer version of bash while preserving the original version and insert the location of the newer version into the PATH variable. The use of #!/bin/bash would result in using the original bash, while #!/usr/bin/env bash would make use of the newer version.
Exit codes
Every command returns an exit code (return status or exit status). A successful command always returns 0 (zero-code), and a command that has failed returns a non-zero value (error code). Failure codes must be positive integers between 1 and 255.
Another handy command we can use when writing a script is exit. This command is used to terminate the current execution and deliver an exit code to the shell. Running an exit code without any arguments, will terminate the running script and return the exit code of the last command executed before exit.
When a program terminates, the shell assigns its exit code to the $? environment variable. The $? variable is how we usually test whether a script has succeeded or not in its execution.
In the same way we can use exit to terminate a script, we can use the return command to exit a function and return an exit code to the caller. You can use exit inside a function too and this will exit the function and terminate the program.
Comments
Scripts may contain comments. Comments are special statements ignored by the shell interpreter. They begin with a # symbol and continue on to the end of the line.
For example:
#!/bin/bash # This script will print your username. whoami
Tip: Use comments to explain what your script does and why.
Variables
Like in most programming languages, you can also create variables in bash.
Bash knows no data types. Variables can contain only numbers or a string of one or more characters. There are three kinds of variables you can create: local variables, environment variables and variables as positional parameters.
Local variables
Local variables are variables that exist only within a single script. They are inaccessible to other programs and scripts.
A local variable can be declared using = sign (as a rule, there should not be any spaces between a variable’s name, = and its value) and its value can be retrieved using the $ sign. For example:
username="denysdovhan" # declare variable echo $username # display value unset username # delete variable
We can also declare a variable local to a single function using the local keyword. Doing so causes the variable to disappear when the function exits.
local local_var="I'm a local value"
Environment variables
Environment variables are variables accessible to any program or script running in current shell session. They are created just like local variables, but using the keyword export instead.
export GLOBAL_VAR="I'm a global variable"
There are a lot of global variables in bash. You will meet these variables fairly often, so here is a quick lookup table with the most practical ones:
| Variable | Description |
|---|---|
$HOME |
The current user’s home directory. |
$PATH |
A colon-separated list of directories in which the shell looks for commands. |
$PWD |
The current working directory. |
$RANDOM |
Random integer between 0 and 32767. |
$UID |
The numeric, real user ID of the current user. |
$PS1 |
The primary prompt string. |
$PS2 |
The secondary prompt string. |
Follow this link to see an extended list of environment variables in Bash.
Positional parameters
Positional parameters are variables allocated when a function is evaluated and are given positionally. The following table lists positional parameter variables and other special variables and their meanings when you are inside a function.
| Parameter | Description |
|---|---|
$0 |
Script’s name. |
$1 … $9 |
The parameter list elements from 1 to 9. |
${10} … ${N} |
The parameter list elements from 10 to N. |
$* or $@ |
All positional parameters except $0. |
$# |
The number of parameters, not counting $0. |
$FUNCNAME |
The function name (has a value only inside a function). |
In the example below, the positional parameters will be $0='./script.sh', $1='foo' and $2='bar':
Variables may also have default values. We can define as such using the following syntax:
# if variables are empty, assign them default values : ${VAR:='default'} : ${1:='first'} # or FOO=${FOO:-'default'}
Shell expansions
Expansions are performed on the command line after it has been split into tokens. In other words, these expansions are a mechanism to calculate arithmetical operations, to save results of commands’ executions and so on.
If you are interested, you can read more about shell expansions.
Brace expansion
Brace expansion allows us to generate arbitrary strings. It’s similar to filename expansion. For example:
echo beg{i,a,u}n # begin began begun
Also brace expansions may be used for creating ranges, which are iterated over in loops.
echo {0..5} # 0 1 2 3 4 5 echo {00..8..2} # 00 02 04 06 08
Note: the rule here is {00..8..2} represents {start..end..steps}
echo {0..10..3} # 0 3 6 9
Command substitution
Command substitution allow us to evaluate a command and substitute its value into another command or variable assignment. Command substitution is performed when a command is enclosed by `` or $(). For example, we can use it as follows:
now=`date +%T` # or now=$(date +%T) echo $now # 19:08:26
Arithmetic expansion
In bash we are free to do any arithmetical operations. But the expression must enclosed by $(( )) The format for arithmetic expansions is:
result=$(( ((10 + 5*3) - 7) / 2 )) echo $result # 9
Within arithmetic expansions, variables should generally be used without a $ prefix:
x=4 y=7 echo $(( x + y )) # 11 echo $(( ++x + y++ )) # 12 echo $(( x + y )) # 13
Double and single quotes
There is an important difference between double and single quotes. Inside double quotes variables or command substitutions are expanded. Inside single quotes they are not. For example:
echo "Your home: $HOME" # Your home: /Users/<username> echo 'Your home: $HOME' # Your home: $HOME
Take care to expand local variables and environment variables within quotes if they could contain whitespace. As an innocuous example, consider using echo to print some user input:
INPUT="A string with strange whitespace." echo $INPUT # A string with strange whitespace. echo "$INPUT" # A string with strange whitespace.
The first echo is invoked with 5 separate arguments — $INPUT is split into separate words, echo prints a single space character between each. In the second case, echo is invoked with a single argument (the entire $INPUT value, including whitespace).
Now consider a more serious example:
FILE="Favorite Things.txt" cat $FILE # attempts to print 2 files: `Favorite` and `Things.txt` cat "$FILE" # prints 1 file: `Favorite Things.txt`
While the issue in this example could be resolved by renaming FILE to Favorite-Things.txt, consider input coming from an environment variable, a positional parameter, or the output of another command (find, cat, etc). If the input might contain whitespace, take care to wrap the expansion in quotes.
Arrays
Like in other programming languages, an array in bash is a variable that allows you to refer to multiple values. In bash, arrays are also zero-based, that is, the first element in an array has index 0.
When dealing with arrays, we should be aware of the special environment variable IFS. IFS, or Input Field Separator, is the character that separates elements in an array. The default value is an empty space IFS=' '.
Array declaration
In bash you create an array by simply assigning a value to an index in the array variable:
fruits[0]=Apple fruits[1]=Pear fruits[2]=Plum
Array variables can also be created using compound assignments such as:
Array expansion
Individual array elements are expanded similar to other variables:
The entire array can be expanded by using * or @ in place of the numeric index:
echo ${fruits[*]} # Apple Pear Plum echo ${fruits[@]} # Apple Pear Plum
There is an important (and subtle) difference between the two lines above: consider an array element containing whitespace:
fruits[0]=Apple
fruits[1]="Desert fig"
fruits[2]=Plum
We want to print each element of the array on a separate line, so we try to use the printf builtin:
printf "+ %sn" ${fruits[*]} # + Apple # + Desert # + fig # + Plum
Why were Desert and fig printed on separate lines? Let’s try to use quoting:
printf "+ %sn" "${fruits[*]}" # + Apple Desert fig Plum
Now everything is on one line — that’s not what we wanted! Here’s where ${fruits[@]} comes into play:
printf "+ %sn" "${fruits[@]}" # + Apple # + Desert fig # + Plum
Within double quotes, ${fruits[@]} expands to a separate argument for each element in the array; whitespace in the array elements is preserved.
Array slice
Besides, we can extract a slice of array using the slice operators:
echo ${fruits[@]:0:2} # Apple Desert fig
In the example above, ${fruits[@]} expands to the entire contents of the array, and :0:2 extracts the slice of length 2, that starts at index 0.
Adding elements into an array
Adding elements into an array is quite simple too. Compound assignments are specially useful in this case. We can use them like this:
fruits=(Orange "${fruits[@]}" Banana Cherry) echo ${fruits[@]} # Orange Apple Desert fig Plum Banana Cherry
The example above, ${fruits[@]} expands to the entire contents of the array and substitutes it into the compound assignment, then assigns the new value into the fruits array mutating its original value.
Deleting elements from an array
To delete an element from an array, use the unset command:
unset fruits[0] echo ${fruits[@]} # Apple Desert fig Plum Banana Cherry
Streams, pipes and lists
Bash has powerful tools for working with other programs and their outputs. Using streams we can send the output of a program into another program or file and thereby write logs or whatever we want.
Pipes give us opportunity to create conveyors and control the execution of commands.
It is paramount we understand how to use this powerful and sophisticated tool.
Streams
Bash receives input and sends output as sequences or streams of characters. These streams may be redirected into files or one into another.
There are three descriptors:
| Code | Descriptor | Description |
|---|---|---|
0 |
stdin |
The standard input. |
1 |
stdout |
The standard output. |
2 |
stderr |
The errors output. |
Redirection makes it possible to control where the output of a command goes to, and where the input of a command comes from. For redirecting streams these operators are used:
| Operator | Description |
|---|---|
> |
Redirecting output |
&> |
Redirecting output and error output |
&>> |
Appending redirected output and error output |
< |
Redirecting input |
<< |
Here documents syntax |
<<< |
Here strings |
Here are few examples of using redirections:
# output of ls will be written to list.txt ls -l > list.txt # append output to list.txt ls -a >> list.txt # all errors will be written to errors.txt grep da * 2> errors.txt # read from errors.txt less < errors.txt
Pipes
We could redirect standard streams not only in files, but also to other programs. Pipes let us use the output of a program as the input of another.
In the example below, command1 sends its output to command2, which then passes it on to the input of command3:
command1 | command2 | command3
Constructions like this are called pipelines.
In practice, this can be used to process data through several programs. For example, here the output of ls -l is sent to the grep program, which prints only files with a .md extension, and this output is finally sent to the less program:
The exit status of a pipeline is normally the exit status of the last command in the pipeline. The shell will not return a status until all the commands in the pipeline have completed. If you want your pipelines to be considered a failure if any of the commands in the pipeline fail, you should set the pipefail option with:
Lists of commands
A list of commands is a sequence of one or more pipelines separated by ;, &, && or || operator.
If a command is terminated by the control operator &, the shell executes the command asynchronously in a subshell. In other words, this command will be executed in the background.
Commands separated by a ; are executed sequentially: one after another. The shell waits for the finish of each command.
# command2 will be executed after command1 command1 ; command2 # which is the same as command1 command2
Lists separated by && and || are called AND and OR lists, respectively.
The AND-list looks like this:
# command2 will be executed if, and only if, command1 finishes successfully (returns 0 exit status) command1 && command2
The OR-list has the form:
# command2 will be executed if, and only if, command1 finishes unsuccessfully (returns code of error) command1 || command2
The return code of an AND or OR list is the exit status of the last executed command.
Conditional statements
Like in other languages, Bash conditionals let us decide to perform an action or not. The result is determined by evaluating an expression, which should be enclosed in [[ ]].
Conditional expression may contain && and || operators, which are AND and OR accordingly. Besides this, there many other handy expressions.
There are two different conditional statements: if statement and case statement.
Primary and combining expressions
Expressions enclosed inside [[ ]] (or [ ] for sh) are called test commands or primaries. These expressions help us to indicate results of a conditional. In the tables below, we are using [ ], because it works for sh too. Here is an answer about the difference between double and single square brackets in bash.
Working with the file system:
| Primary | Meaning |
|---|---|
[ -e FILE ] |
True if FILE exists. |
[ -f FILE ] |
True if FILE exists and is a regular file. |
[ -d FILE ] |
True if FILE exists and is a directory. |
[ -s FILE ] |
True if FILE exists and not empty (size more than 0). |
[ -r FILE ] |
True if FILE exists and is readable. |
[ -w FILE ] |
True if FILE exists and is writable. |
[ -x FILE ] |
True if FILE exists and is executable. |
[ -L FILE ] |
True if FILE exists and is symbolic link. |
[ FILE1 -nt FILE2 ] |
FILE1 is newer than FILE2. |
[ FILE1 -ot FILE2 ] |
FILE1 is older than FILE2. |
Working with strings:
| Primary | Meaning |
|---|---|
[ -z STR ] |
STR is empty (the length is zero). |
[ -n STR ] |
STR is not empty (the length is non-zero). |
[ STR1 == STR2 ] |
STR1 and STR2 are equal. |
[ STR1 != STR2 ] |
STR1 and STR2 are not equal. |
Arithmetic binary operators:
| Primary | Meaning |
|---|---|
[ ARG1 -eq ARG2 ] |
ARG1 is equal to ARG2. |
[ ARG1 -ne ARG2 ] |
ARG1 is not equal to ARG2. |
[ ARG1 -lt ARG2 ] |
ARG1 is less than ARG2. |
[ ARG1 -le ARG2 ] |
ARG1 is less than or equal to ARG2. |
[ ARG1 -gt ARG2 ] |
ARG1 is greater than ARG2. |
[ ARG1 -ge ARG2 ] |
ARG1 is greater than or equal to ARG2. |
Conditions may be combined using these combining expressions:
| Operation | Effect |
|---|---|
[ ! EXPR ] |
True if EXPR is false. |
[ (EXPR) ] |
Returns the value of EXPR. |
[ EXPR1 -a EXPR2 ] |
Logical AND. True if EXPR1 and EXPR2 are true. |
[ EXPR1 -o EXPR2 ] |
Logical OR. True if EXPR1 or EXPR2 are true. |
Sure, there are more useful primaries and you can easily find them in the Bash man pages.
Using an if statement
if statements work the same as in other programming languages. If the expression within the braces is true, the code between then and fi is executed. fi indicates the end of the conditionally executed code.
# Single-line if [[ 1 -eq 1 ]]; then echo "true"; fi # Multi-line if [[ 1 -eq 1 ]]; then echo "true" fi
Likewise, we could use an if..else statement such as:
# Single-line if [[ 2 -ne 1 ]]; then echo "true"; else echo "false"; fi # Multi-line if [[ 2 -ne 1 ]]; then echo "true" else echo "false" fi
Sometimes if..else statements are not enough to do what we want to do. In this case we shouldn’t forget about the existence of if..elif..else statements, which always come in handy.
Look at the example below:
if [[ `uname` == "Adam" ]]; then echo "Do not eat an apple!" elif [[ `uname` == "Eva" ]]; then echo "Do not take an apple!" else echo "Apples are delicious!" fi
Using a case statement
If you are confronted with a couple of different possible actions to take, then using a case statement may be more useful than nested if statements. For more complex conditions use case like below:
case "$extension" in "jpg"|"jpeg") echo "It's image with jpeg extension." ;; "png") echo "It's image with png extension." ;; "gif") echo "Oh, it's a giphy!" ;; *) echo "Woops! It's not image!" ;; esac
Each case is an expression matching a pattern. The | sign is used for separating multiple patterns, and the ) operator terminates a pattern list. The commands for the first match are executed. * is the pattern for anything else that doesn’t match the defined patterns. Each block of commands should be divided with the ;; operator.
Loops
Here we won’t be surprised. As in any programming language, a loop in bash is a block of code that iterates as long as the control conditional is true.
There are four types of loops in Bash: for, while, until and select.
for loop
The for is very similar to its sibling in C. It looks like this:
for arg in elem1 elem2 ... elemN do # statements done
During each pass through the loop, arg takes on the value from elem1 to elemN. Values may also be wildcards or brace expansions.
Also, we can write for loop in one line, but in this case there needs to be a semicolon before do, like below:
for i in {1..5}; do echo $i; done
By the way, if for..in..do seems a little bit weird to you, you can also write for in C-like style such as:
for (( i = 0; i < 10; i++ )); do echo $i done
for is handy when we want to do the same operation over each file in a directory. For example, if we need to move all .bash files into the script folder and then give them execute permissions, our script would look like this:
#!/bin/bash for FILE in $HOME/*.bash; do mv "$FILE" "${HOME}/scripts" chmod +x "${HOME}/scripts/${FILE}" done
while loop
The while loop tests a condition and loops over a sequence of commands so long as that condition is true. A condition is nothing more than a primary as used in if..then conditions. So a while loop looks like this:
while [[ condition ]] do # statements done
Just like in the case of the for loop, if we want to write do and condition in the same line, then we must use a semicolon before do.
A working example might look like this:
#!/bin/bash # Squares of numbers from 0 through 9 x=0 while [[ $x -lt 10 ]]; do # value of x is less than 10 echo $(( x * x )) x=$(( x + 1 )) # increase x done
until loop
The until loop is the exact opposite of the while loop. Like a while it checks a test condition, but it keeps looping as long as this condition is false:
until [[ condition ]]; do #statements done
select loop
The select loop helps us to organize a user menu. It has almost the same syntax as the for loop:
select answer in elem1 elem2 ... elemN do # statements done
The select prints all elem1..elemN on the screen with their sequence numbers, after that it prompts the user. Usually it looks like $? (PS3 variable). The answer will be saved in answer. If answer is the number between 1..N, then statements will execute and select will go to the next iteration — that’s because we should use the break statement.
A working example might look like this:
#!/bin/bash PS3="Choose the package manager: " select ITEM in bower npm gem pip do echo -n "Enter the package name: " && read PACKAGE case $ITEM in bower) bower install $PACKAGE ;; npm) npm install $PACKAGE ;; gem) gem install $PACKAGE ;; pip) pip install $PACKAGE ;; esac break # avoid infinite loop done
This example, asks the user what package manager {s,he} would like to use. Then, it will ask what package we want to install and finally proceed to install it.
If we run this, we will get:
$ ./my_script
1) bower
2) npm
3) gem
4) pip
Choose the package manager: 2
Enter the package name: bash-handbook
<installing bash-handbook>
Loop control
There are situations when we need to stop a loop before its normal ending or step over an iteration. In these cases, we can use the shell built-in break and continue statements. Both of these work with every kind of loop.
The break statement is used to exit the current loop before its ending. We have already met with it.
The continue statement steps over one iteration. We can use it as such:
for (( i = 0; i < 10; i++ )); do if [[ $(( i % 2 )) -eq 0 ]]; then continue; fi echo $i done
If we run the example above, it will print all odd numbers from 0 through 9.
Functions
In scripts we have the ability to define and call functions. As in any programming language, functions in bash are chunks of code, but there are differences.
In bash, functions are a sequence of commands grouped under a single name, that is the name of the function. Calling a function is the same as calling any other program, you just write the name and the function will be invoked.
We can declare our own function this way:
my_func () { # statements } my_func # call my_func
We must declare functions before we can invoke them.
Functions can take on arguments and return a result — exit code. Arguments, within functions, are treated in the same manner as arguments given to the script in non-interactive mode — using positional parameters. A result code can be returned using the return command.
Below is a function that takes a name and returns 0, indicating successful execution.
# function with params greeting () { if [[ -n $1 ]]; then echo "Hello, $1!" else echo "Hello, unknown!" fi return 0 } greeting Denys # Hello, Denys! greeting # Hello, unknown!
We already discussed exit codes. The return command without any arguments returns the exit code of the last executed command. Above, return 0 will return a successful exit code. 0.
Debugging
The shell gives us tools for debugging scripts. If we want to run a script in debug mode, we use a special option in our script’s shebang:
These options are settings that change shell behavior. The following table is a list of options which might be useful to you:
| Short | Name | Description |
|---|---|---|
-f |
noglob | Disable filename expansion (globbing). |
-i |
interactive | Script runs in interactive mode. |
-n |
noexec | Read commands, but don’t execute them (syntax check). |
| pipefail | Make pipelines fail if any commands fail, not just if the final command fail. | |
-t |
— | Exit after first command. |
-v |
verbose | Print each command to stderr before executing it. |
-x |
xtrace | Print each command and its expanded arguments to stderr before executing it. |
For example, we have script with -x option such as:
#!/bin/bash -x for (( i = 0; i < 3; i++ )); do echo $i done
This will print the value of the variables to stdout along with other useful information:
$ ./my_script
+ (( i = 0 ))
+ (( i < 3 ))
+ echo 0
0
+ (( i++ ))
+ (( i < 3 ))
+ echo 1
1
+ (( i++ ))
+ (( i < 3 ))
+ echo 2
2
+ (( i++ ))
+ (( i < 3 ))
Sometimes we need to debug a part of a script. In this case using the set command is convenient. This command can enable and disable options. Options are turned on using - and turned off using +:
#!/bin/bash echo "xtrace is turned off" set -x echo "xtrace is enabled" set +x echo "xtrace is turned off again"
Reading User Input
The user can enter data into shell variables using read commands.
read
This command reads input from stdin into variables
read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars]
[-p prompt] [-t timeout] [-u fd] [variable1 ...] [variable2 ...]
If no variable names are provided, the user input is stored in the variable $REPLY by default.
#!/bin/bash read #Waits for user input echo $REPLY #Prints the text
| Short | Name | Description |
|---|---|---|
-a |
array | Store the words in an indexed array named $array |
-e |
Read data from terminal character by character till the delimiter is reached | |
-d |
delimiter | Set the delimiting character to delimiter specified By default, newline(‘n’) is the delimiter |
-n |
nchars | Stop reading when n characters or delimiter is read |
-N |
nchars | Stops reading only when n charaters or EOF is read, ignores delimiter |
-p |
prompt | Prints prompt string on console |
-i |
interactive | Prints placeholder text which user can modify Used in conjunction with -e |
-r |
raw input | Disable shell interpretation of special charaters like $ and * |
-s |
silent | Disable echo of characters read onto terminal |
-t |
timout | Waits for certain amount of time before exitting |
-u |
file descriptor | Reads input from file descriptor specified |
#!/bin/bash read -p 'Enter your name: ' name echo Hello, "$name"! read -sp 'Enter Password: ' password if [ "$password" == "1234" ]; then #Space around '[' is required echo -e "nSuccess!" else echo -e "nTry Again!" fi echo "" read -p "Enter numbers: " -a array echo ${array[2]}
This script will produce such output:
Enter your name: User1
Enter password:
Success #if password entered was 1234
Enter numbers: 100 200 300 400
300
Afterword
I hope this small handbook was interesting and helpful. To be honest, I wrote this handbook for myself so as to not forget the bash basics. I tried to write concisely but meaningfully, and I hope you will appreciate that.
This handbook narrates my own experience with Bash. It does not purport to be comprehensive, so if you still want more, please run man bash and start there.
Contributions are absolutely welcome and I will be grateful for any corrections or questions you can send my way. For all of that create a new issue.
Thanks for reading this handbook!
Want to learn more?
Here’s a list of other literature covering Bash:
- Bash man page. In many environments that you can run Bash, the help system
mancan display information about Bash, by running the commandman bash. For more information on themancommand, see the web page «The man Command» hosted at The Linux Information Project. - «Bourne-Again SHell manual» in many formats, including HTML, Info, TeX, PDF, and Texinfo. Hosted at https://www.gnu.org/. As of 2016/01, this covers version 4.3, last updated 2015/02/02.
- «Bash Shell Script» to learn how to write scripts using bash. It is hosted at https://www.gnu.org/.
Other resources
- awesome-bash is a curated list of Bash scripts and resources
- awesome-shell is another curated list of shell resources
- bash-it provides a solid framework for using, developing and maintaining shell scripts and custom commands for your daily work.
- Bash Guide for Beginners a good resource between the HOWTO and the Bash Scripting guide.
- dotfiles.github.io is a good source of pointers to the various dotfiles collections and shell frameworks available for bash and other shells.
- learnyoubash helps you write your first bash script
- shellcheck is a static analysis tool for shell scripts. You can either use it from a web page at www.shellcheck.net or run it from the command line. Installation instructions are on the koalaman/shellcheck github repository page.
Finally, Stack Overflow has many questions that are tagged as bash that you can learn from and is a good place to ask if you’re stuck.
License
© Denys Dovhan
bash-handbook
This document is written for those who want to learn Bash without diving in too deeply.
Tip: Try learnyoubash — an interactive workshopper based on this handbook!
Node Packaged Manuscript
You can install this handbook using npm. Just run:
$ npm install -g bash-handbook
You should be able to run bash-handbook at the command line now. This will open the manual in your selected $PAGER. Otherwise, you may continue reading on here.
The source is available here: https://github.com/denysdovhan/bash-handbook
Translations
Currently, there are these translations of bash-handbook:
- Português (Brasil)
- 简体中文 (中国)
- 繁體中文(台灣)
- 한국어 (한국)
Request another translation
Table of Contents
- Introduction
- Shells and modes
- Interactive mode
- Non-interactive mode
- Exit codes
- Comments
- Variables
- Local variables
- Environment variables
- Positional parameters
- Shell expansions
- Brace expansion
- Command substitution
- Arithmetic expansion
- Double and single quotes
- Arrays
- Array declaration
- Array expansion
- Array slice
- Adding elements into an array
- Deleting elements from an array
- Streams, pipes and lists
- Streams
- Pipes
- Lists of commands
- Conditional statements
- Primary and combining expressions
- Using an
ifstatement - Using a
casestatement
- Loops
forloopwhileloopuntilloopselectloop- Loop control
- Functions
- Debugging
- Reading User Input
- Afterword
- Want to learn more?
- Other resources
- License
Introduction
If you are a developer, then you know the value of time. Optimizing your work process is one of the most important aspects of the job.
In that path towards efficiency and productivity, we are often posed with actions that must be repeated over and over again, like:
- taking a screenshot and uploading it to a server
- processing text that may come in many shapes and forms
- converting files between different formats
- parsing a program’s output
Enter Bash, our savior.
Bash is a Unix shell written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. It was released in 1989 and has been distributed as the Linux and macOS default shell for a long time.
So why do we need to learn something that was written more than 30 years ago? The answer is simple: this something is today one of the most powerful and portable tools for writing efficient scripts for all Unix-based systems. And that’s why you should learn bash. Period.
In this handbook, I’m going to describe the most important concepts in bash with examples. I hope this compendium will be helpful to you.
Shells and modes
The user bash shell can work in two modes — interactive and non-interactive.
Interactive mode
If you are working on Ubuntu, you have seven virtual terminals available to you.
The desktop environment takes place in the seventh virtual terminal, so you can return to a friendly GUI
using the Ctrl-Alt-F7 keybinding.
You can open the shell using the Ctrl-Alt-F1 keybinding. After that, the familiar GUI will disappear and one of the virtual terminals will be shown.
If you see something like this, then you are working in interactive mode:
Here you can enter a variety of Unix commands, such as ls, grep, cd, mkdir, rm and see the result of their execution.
We call this shell interactive because it interacts directly with the user.
Using a virtual terminal is not really convenient. For example, if you want to edit a document and execute another command at the same time, you are better off using virtual terminal emulators like:
- GNOME Terminal
- Terminator
- iTerm2
- ConEmu
Non-interactive mode
In non-interactive mode, the shell reads commands from a file or a pipe and executes them. When the interpreter reaches the end of the file, the shell process terminates the session and returns to the parent process.
Use the following commands for running the shell in non-interactive mode:
sh /path/to/script.sh
bash /path/to/script.sh
In the example above, script.sh is just a regular text file that consists of commands the shell interpreter can evaluate and sh or bash is the shell’s interpreter program. You can create script.sh using your preferred text editor (e.g. vim, nano, Sublime Text, Atom, etc).
You can also simplify invoking the script by making it an executable file using the chmod command:
chmod +x /path/to/script.sh
Additionally, the first line in the script must indicate which program it should use to run the file, like so:
#!/bin/bash echo "Hello, world!"
Or if you prefer to use sh instead of bash, change #!/bin/bash to #!/bin/sh. This #! character sequence is known as the shebang. Now you can run the script like this:
A handy trick we used above is using echo to print text to the terminal screen.
Another way to use the shebang line is as follows:
#!/usr/bin/env bash echo "Hello, world!"
The advantage of this shebang line is it will search for the program (in this case bash) based on the PATH environment variable. This is often preferred over the first method shown above, as the location of a program on a filesystem cannot always be assumed. This is also useful if the PATH variable on a system has been configured to point to an alternate version of the program. For instance, one might install a newer version of bash while preserving the original version and insert the location of the newer version into the PATH variable. The use of #!/bin/bash would result in using the original bash, while #!/usr/bin/env bash would make use of the newer version.
Exit codes
Every command returns an exit code (return status or exit status). A successful command always returns 0 (zero-code), and a command that has failed returns a non-zero value (error code). Failure codes must be positive integers between 1 and 255.
Another handy command we can use when writing a script is exit. This command is used to terminate the current execution and deliver an exit code to the shell. Running an exit code without any arguments, will terminate the running script and return the exit code of the last command executed before exit.
When a program terminates, the shell assigns its exit code to the $? environment variable. The $? variable is how we usually test whether a script has succeeded or not in its execution.
In the same way we can use exit to terminate a script, we can use the return command to exit a function and return an exit code to the caller. You can use exit inside a function too and this will exit the function and terminate the program.
Comments
Scripts may contain comments. Comments are special statements ignored by the shell interpreter. They begin with a # symbol and continue on to the end of the line.
For example:
#!/bin/bash # This script will print your username. whoami
Tip: Use comments to explain what your script does and why.
Variables
Like in most programming languages, you can also create variables in bash.
Bash knows no data types. Variables can contain only numbers or a string of one or more characters. There are three kinds of variables you can create: local variables, environment variables and variables as positional parameters.
Local variables
Local variables are variables that exist only within a single script. They are inaccessible to other programs and scripts.
A local variable can be declared using = sign (as a rule, there should not be any spaces between a variable’s name, = and its value) and its value can be retrieved using the $ sign. For example:
username="denysdovhan" # declare variable echo $username # display value unset username # delete variable
We can also declare a variable local to a single function using the local keyword. Doing so causes the variable to disappear when the function exits.
local local_var="I'm a local value"
Environment variables
Environment variables are variables accessible to any program or script running in current shell session. They are created just like local variables, but using the keyword export instead.
export GLOBAL_VAR="I'm a global variable"
There are a lot of global variables in bash. You will meet these variables fairly often, so here is a quick lookup table with the most practical ones:
| Variable | Description |
|---|---|
$HOME |
The current user’s home directory. |
$PATH |
A colon-separated list of directories in which the shell looks for commands. |
$PWD |
The current working directory. |
$RANDOM |
Random integer between 0 and 32767. |
$UID |
The numeric, real user ID of the current user. |
$PS1 |
The primary prompt string. |
$PS2 |
The secondary prompt string. |
Follow this link to see an extended list of environment variables in Bash.
Positional parameters
Positional parameters are variables allocated when a function is evaluated and are given positionally. The following table lists positional parameter variables and other special variables and their meanings when you are inside a function.
| Parameter | Description |
|---|---|
$0 |
Script’s name. |
$1 … $9 |
The parameter list elements from 1 to 9. |
${10} … ${N} |
The parameter list elements from 10 to N. |
$* or $@ |
All positional parameters except $0. |
$# |
The number of parameters, not counting $0. |
$FUNCNAME |
The function name (has a value only inside a function). |
In the example below, the positional parameters will be $0='./script.sh', $1='foo' and $2='bar':
Variables may also have default values. We can define as such using the following syntax:
# if variables are empty, assign them default values : ${VAR:='default'} : ${1:='first'} # or FOO=${FOO:-'default'}
Shell expansions
Expansions are performed on the command line after it has been split into tokens. In other words, these expansions are a mechanism to calculate arithmetical operations, to save results of commands’ executions and so on.
If you are interested, you can read more about shell expansions.
Brace expansion
Brace expansion allows us to generate arbitrary strings. It’s similar to filename expansion. For example:
echo beg{i,a,u}n # begin began begun
Also brace expansions may be used for creating ranges, which are iterated over in loops.
echo {0..5} # 0 1 2 3 4 5 echo {00..8..2} # 00 02 04 06 08
Note: the rule here is {00..8..2} represents {start..end..steps}
echo {0..10..3} # 0 3 6 9
Command substitution
Command substitution allow us to evaluate a command and substitute its value into another command or variable assignment. Command substitution is performed when a command is enclosed by `` or $(). For example, we can use it as follows:
now=`date +%T` # or now=$(date +%T) echo $now # 19:08:26
Arithmetic expansion
In bash we are free to do any arithmetical operations. But the expression must enclosed by $(( )) The format for arithmetic expansions is:
result=$(( ((10 + 5*3) - 7) / 2 )) echo $result # 9
Within arithmetic expansions, variables should generally be used without a $ prefix:
x=4 y=7 echo $(( x + y )) # 11 echo $(( ++x + y++ )) # 12 echo $(( x + y )) # 13
Double and single quotes
There is an important difference between double and single quotes. Inside double quotes variables or command substitutions are expanded. Inside single quotes they are not. For example:
echo "Your home: $HOME" # Your home: /Users/<username> echo 'Your home: $HOME' # Your home: $HOME
Take care to expand local variables and environment variables within quotes if they could contain whitespace. As an innocuous example, consider using echo to print some user input:
INPUT="A string with strange whitespace." echo $INPUT # A string with strange whitespace. echo "$INPUT" # A string with strange whitespace.
The first echo is invoked with 5 separate arguments — $INPUT is split into separate words, echo prints a single space character between each. In the second case, echo is invoked with a single argument (the entire $INPUT value, including whitespace).
Now consider a more serious example:
FILE="Favorite Things.txt" cat $FILE # attempts to print 2 files: `Favorite` and `Things.txt` cat "$FILE" # prints 1 file: `Favorite Things.txt`
While the issue in this example could be resolved by renaming FILE to Favorite-Things.txt, consider input coming from an environment variable, a positional parameter, or the output of another command (find, cat, etc). If the input might contain whitespace, take care to wrap the expansion in quotes.
Arrays
Like in other programming languages, an array in bash is a variable that allows you to refer to multiple values. In bash, arrays are also zero-based, that is, the first element in an array has index 0.
When dealing with arrays, we should be aware of the special environment variable IFS. IFS, or Input Field Separator, is the character that separates elements in an array. The default value is an empty space IFS=' '.
Array declaration
In bash you create an array by simply assigning a value to an index in the array variable:
fruits[0]=Apple fruits[1]=Pear fruits[2]=Plum
Array variables can also be created using compound assignments such as:
Array expansion
Individual array elements are expanded similar to other variables:
The entire array can be expanded by using * or @ in place of the numeric index:
echo ${fruits[*]} # Apple Pear Plum echo ${fruits[@]} # Apple Pear Plum
There is an important (and subtle) difference between the two lines above: consider an array element containing whitespace:
fruits[0]=Apple
fruits[1]="Desert fig"
fruits[2]=Plum
We want to print each element of the array on a separate line, so we try to use the printf builtin:
printf "+ %sn" ${fruits[*]} # + Apple # + Desert # + fig # + Plum
Why were Desert and fig printed on separate lines? Let’s try to use quoting:
printf "+ %sn" "${fruits[*]}" # + Apple Desert fig Plum
Now everything is on one line — that’s not what we wanted! Here’s where ${fruits[@]} comes into play:
printf "+ %sn" "${fruits[@]}" # + Apple # + Desert fig # + Plum
Within double quotes, ${fruits[@]} expands to a separate argument for each element in the array; whitespace in the array elements is preserved.
Array slice
Besides, we can extract a slice of array using the slice operators:
echo ${fruits[@]:0:2} # Apple Desert fig
In the example above, ${fruits[@]} expands to the entire contents of the array, and :0:2 extracts the slice of length 2, that starts at index 0.
Adding elements into an array
Adding elements into an array is quite simple too. Compound assignments are specially useful in this case. We can use them like this:
fruits=(Orange "${fruits[@]}" Banana Cherry) echo ${fruits[@]} # Orange Apple Desert fig Plum Banana Cherry
The example above, ${fruits[@]} expands to the entire contents of the array and substitutes it into the compound assignment, then assigns the new value into the fruits array mutating its original value.
Deleting elements from an array
To delete an element from an array, use the unset command:
unset fruits[0] echo ${fruits[@]} # Apple Desert fig Plum Banana Cherry
Streams, pipes and lists
Bash has powerful tools for working with other programs and their outputs. Using streams we can send the output of a program into another program or file and thereby write logs or whatever we want.
Pipes give us opportunity to create conveyors and control the execution of commands.
It is paramount we understand how to use this powerful and sophisticated tool.
Streams
Bash receives input and sends output as sequences or streams of characters. These streams may be redirected into files or one into another.
There are three descriptors:
| Code | Descriptor | Description |
|---|---|---|
0 |
stdin |
The standard input. |
1 |
stdout |
The standard output. |
2 |
stderr |
The errors output. |
Redirection makes it possible to control where the output of a command goes to, and where the input of a command comes from. For redirecting streams these operators are used:
| Operator | Description |
|---|---|
> |
Redirecting output |
&> |
Redirecting output and error output |
&>> |
Appending redirected output and error output |
< |
Redirecting input |
<< |
Here documents syntax |
<<< |
Here strings |
Here are few examples of using redirections:
# output of ls will be written to list.txt ls -l > list.txt # append output to list.txt ls -a >> list.txt # all errors will be written to errors.txt grep da * 2> errors.txt # read from errors.txt less < errors.txt
Pipes
We could redirect standard streams not only in files, but also to other programs. Pipes let us use the output of a program as the input of another.
In the example below, command1 sends its output to command2, which then passes it on to the input of command3:
command1 | command2 | command3
Constructions like this are called pipelines.
In practice, this can be used to process data through several programs. For example, here the output of ls -l is sent to the grep program, which prints only files with a .md extension, and this output is finally sent to the less program:
The exit status of a pipeline is normally the exit status of the last command in the pipeline. The shell will not return a status until all the commands in the pipeline have completed. If you want your pipelines to be considered a failure if any of the commands in the pipeline fail, you should set the pipefail option with:
Lists of commands
A list of commands is a sequence of one or more pipelines separated by ;, &, && or || operator.
If a command is terminated by the control operator &, the shell executes the command asynchronously in a subshell. In other words, this command will be executed in the background.
Commands separated by a ; are executed sequentially: one after another. The shell waits for the finish of each command.
# command2 will be executed after command1 command1 ; command2 # which is the same as command1 command2
Lists separated by && and || are called AND and OR lists, respectively.
The AND-list looks like this:
# command2 will be executed if, and only if, command1 finishes successfully (returns 0 exit status) command1 && command2
The OR-list has the form:
# command2 will be executed if, and only if, command1 finishes unsuccessfully (returns code of error) command1 || command2
The return code of an AND or OR list is the exit status of the last executed command.
Conditional statements
Like in other languages, Bash conditionals let us decide to perform an action or not. The result is determined by evaluating an expression, which should be enclosed in [[ ]].
Conditional expression may contain && and || operators, which are AND and OR accordingly. Besides this, there many other handy expressions.
There are two different conditional statements: if statement and case statement.
Primary and combining expressions
Expressions enclosed inside [[ ]] (or [ ] for sh) are called test commands or primaries. These expressions help us to indicate results of a conditional. In the tables below, we are using [ ], because it works for sh too. Here is an answer about the difference between double and single square brackets in bash.
Working with the file system:
| Primary | Meaning |
|---|---|
[ -e FILE ] |
True if FILE exists. |
[ -f FILE ] |
True if FILE exists and is a regular file. |
[ -d FILE ] |
True if FILE exists and is a directory. |
[ -s FILE ] |
True if FILE exists and not empty (size more than 0). |
[ -r FILE ] |
True if FILE exists and is readable. |
[ -w FILE ] |
True if FILE exists and is writable. |
[ -x FILE ] |
True if FILE exists and is executable. |
[ -L FILE ] |
True if FILE exists and is symbolic link. |
[ FILE1 -nt FILE2 ] |
FILE1 is newer than FILE2. |
[ FILE1 -ot FILE2 ] |
FILE1 is older than FILE2. |
Working with strings:
| Primary | Meaning |
|---|---|
[ -z STR ] |
STR is empty (the length is zero). |
[ -n STR ] |
STR is not empty (the length is non-zero). |
[ STR1 == STR2 ] |
STR1 and STR2 are equal. |
[ STR1 != STR2 ] |
STR1 and STR2 are not equal. |
Arithmetic binary operators:
| Primary | Meaning |
|---|---|
[ ARG1 -eq ARG2 ] |
ARG1 is equal to ARG2. |
[ ARG1 -ne ARG2 ] |
ARG1 is not equal to ARG2. |
[ ARG1 -lt ARG2 ] |
ARG1 is less than ARG2. |
[ ARG1 -le ARG2 ] |
ARG1 is less than or equal to ARG2. |
[ ARG1 -gt ARG2 ] |
ARG1 is greater than ARG2. |
[ ARG1 -ge ARG2 ] |
ARG1 is greater than or equal to ARG2. |
Conditions may be combined using these combining expressions:
| Operation | Effect |
|---|---|
[ ! EXPR ] |
True if EXPR is false. |
[ (EXPR) ] |
Returns the value of EXPR. |
[ EXPR1 -a EXPR2 ] |
Logical AND. True if EXPR1 and EXPR2 are true. |
[ EXPR1 -o EXPR2 ] |
Logical OR. True if EXPR1 or EXPR2 are true. |
Sure, there are more useful primaries and you can easily find them in the Bash man pages.
Using an if statement
if statements work the same as in other programming languages. If the expression within the braces is true, the code between then and fi is executed. fi indicates the end of the conditionally executed code.
# Single-line if [[ 1 -eq 1 ]]; then echo "true"; fi # Multi-line if [[ 1 -eq 1 ]]; then echo "true" fi
Likewise, we could use an if..else statement such as:
# Single-line if [[ 2 -ne 1 ]]; then echo "true"; else echo "false"; fi # Multi-line if [[ 2 -ne 1 ]]; then echo "true" else echo "false" fi
Sometimes if..else statements are not enough to do what we want to do. In this case we shouldn’t forget about the existence of if..elif..else statements, which always come in handy.
Look at the example below:
if [[ `uname` == "Adam" ]]; then echo "Do not eat an apple!" elif [[ `uname` == "Eva" ]]; then echo "Do not take an apple!" else echo "Apples are delicious!" fi
Using a case statement
If you are confronted with a couple of different possible actions to take, then using a case statement may be more useful than nested if statements. For more complex conditions use case like below:
case "$extension" in "jpg"|"jpeg") echo "It's image with jpeg extension." ;; "png") echo "It's image with png extension." ;; "gif") echo "Oh, it's a giphy!" ;; *) echo "Woops! It's not image!" ;; esac
Each case is an expression matching a pattern. The | sign is used for separating multiple patterns, and the ) operator terminates a pattern list. The commands for the first match are executed. * is the pattern for anything else that doesn’t match the defined patterns. Each block of commands should be divided with the ;; operator.
Loops
Here we won’t be surprised. As in any programming language, a loop in bash is a block of code that iterates as long as the control conditional is true.
There are four types of loops in Bash: for, while, until and select.
for loop
The for is very similar to its sibling in C. It looks like this:
for arg in elem1 elem2 ... elemN do # statements done
During each pass through the loop, arg takes on the value from elem1 to elemN. Values may also be wildcards or brace expansions.
Also, we can write for loop in one line, but in this case there needs to be a semicolon before do, like below:
for i in {1..5}; do echo $i; done
By the way, if for..in..do seems a little bit weird to you, you can also write for in C-like style such as:
for (( i = 0; i < 10; i++ )); do echo $i done
for is handy when we want to do the same operation over each file in a directory. For example, if we need to move all .bash files into the script folder and then give them execute permissions, our script would look like this:
#!/bin/bash for FILE in $HOME/*.bash; do mv "$FILE" "${HOME}/scripts" chmod +x "${HOME}/scripts/${FILE}" done
while loop
The while loop tests a condition and loops over a sequence of commands so long as that condition is true. A condition is nothing more than a primary as used in if..then conditions. So a while loop looks like this:
while [[ condition ]] do # statements done
Just like in the case of the for loop, if we want to write do and condition in the same line, then we must use a semicolon before do.
A working example might look like this:
#!/bin/bash # Squares of numbers from 0 through 9 x=0 while [[ $x -lt 10 ]]; do # value of x is less than 10 echo $(( x * x )) x=$(( x + 1 )) # increase x done
until loop
The until loop is the exact opposite of the while loop. Like a while it checks a test condition, but it keeps looping as long as this condition is false:
until [[ condition ]]; do #statements done
select loop
The select loop helps us to organize a user menu. It has almost the same syntax as the for loop:
select answer in elem1 elem2 ... elemN do # statements done
The select prints all elem1..elemN on the screen with their sequence numbers, after that it prompts the user. Usually it looks like $? (PS3 variable). The answer will be saved in answer. If answer is the number between 1..N, then statements will execute and select will go to the next iteration — that’s because we should use the break statement.
A working example might look like this:
#!/bin/bash PS3="Choose the package manager: " select ITEM in bower npm gem pip do echo -n "Enter the package name: " && read PACKAGE case $ITEM in bower) bower install $PACKAGE ;; npm) npm install $PACKAGE ;; gem) gem install $PACKAGE ;; pip) pip install $PACKAGE ;; esac break # avoid infinite loop done
This example, asks the user what package manager {s,he} would like to use. Then, it will ask what package we want to install and finally proceed to install it.
If we run this, we will get:
$ ./my_script
1) bower
2) npm
3) gem
4) pip
Choose the package manager: 2
Enter the package name: bash-handbook
<installing bash-handbook>
Loop control
There are situations when we need to stop a loop before its normal ending or step over an iteration. In these cases, we can use the shell built-in break and continue statements. Both of these work with every kind of loop.
The break statement is used to exit the current loop before its ending. We have already met with it.
The continue statement steps over one iteration. We can use it as such:
for (( i = 0; i < 10; i++ )); do if [[ $(( i % 2 )) -eq 0 ]]; then continue; fi echo $i done
If we run the example above, it will print all odd numbers from 0 through 9.
Functions
In scripts we have the ability to define and call functions. As in any programming language, functions in bash are chunks of code, but there are differences.
In bash, functions are a sequence of commands grouped under a single name, that is the name of the function. Calling a function is the same as calling any other program, you just write the name and the function will be invoked.
We can declare our own function this way:
my_func () { # statements } my_func # call my_func
We must declare functions before we can invoke them.
Functions can take on arguments and return a result — exit code. Arguments, within functions, are treated in the same manner as arguments given to the script in non-interactive mode — using positional parameters. A result code can be returned using the return command.
Below is a function that takes a name and returns 0, indicating successful execution.
# function with params greeting () { if [[ -n $1 ]]; then echo "Hello, $1!" else echo "Hello, unknown!" fi return 0 } greeting Denys # Hello, Denys! greeting # Hello, unknown!
We already discussed exit codes. The return command without any arguments returns the exit code of the last executed command. Above, return 0 will return a successful exit code. 0.
Debugging
The shell gives us tools for debugging scripts. If we want to run a script in debug mode, we use a special option in our script’s shebang:
These options are settings that change shell behavior. The following table is a list of options which might be useful to you:
| Short | Name | Description |
|---|---|---|
-f |
noglob | Disable filename expansion (globbing). |
-i |
interactive | Script runs in interactive mode. |
-n |
noexec | Read commands, but don’t execute them (syntax check). |
| pipefail | Make pipelines fail if any commands fail, not just if the final command fail. | |
-t |
— | Exit after first command. |
-v |
verbose | Print each command to stderr before executing it. |
-x |
xtrace | Print each command and its expanded arguments to stderr before executing it. |
For example, we have script with -x option such as:
#!/bin/bash -x for (( i = 0; i < 3; i++ )); do echo $i done
This will print the value of the variables to stdout along with other useful information:
$ ./my_script
+ (( i = 0 ))
+ (( i < 3 ))
+ echo 0
0
+ (( i++ ))
+ (( i < 3 ))
+ echo 1
1
+ (( i++ ))
+ (( i < 3 ))
+ echo 2
2
+ (( i++ ))
+ (( i < 3 ))
Sometimes we need to debug a part of a script. In this case using the set command is convenient. This command can enable and disable options. Options are turned on using - and turned off using +:
#!/bin/bash echo "xtrace is turned off" set -x echo "xtrace is enabled" set +x echo "xtrace is turned off again"
Reading User Input
The user can enter data into shell variables using read commands.
read
This command reads input from stdin into variables
read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars]
[-p prompt] [-t timeout] [-u fd] [variable1 ...] [variable2 ...]
If no variable names are provided, the user input is stored in the variable $REPLY by default.
#!/bin/bash read #Waits for user input echo $REPLY #Prints the text
| Short | Name | Description |
|---|---|---|
-a |
array | Store the words in an indexed array named $array |
-e |
Read data from terminal character by character till the delimiter is reached | |
-d |
delimiter | Set the delimiting character to delimiter specified By default, newline(‘n’) is the delimiter |
-n |
nchars | Stop reading when n characters or delimiter is read |
-N |
nchars | Stops reading only when n charaters or EOF is read, ignores delimiter |
-p |
prompt | Prints prompt string on console |
-i |
interactive | Prints placeholder text which user can modify Used in conjunction with -e |
-r |
raw input | Disable shell interpretation of special charaters like $ and * |
-s |
silent | Disable echo of characters read onto terminal |
-t |
timout | Waits for certain amount of time before exitting |
-u |
file descriptor | Reads input from file descriptor specified |
#!/bin/bash read -p 'Enter your name: ' name echo Hello, "$name"! read -sp 'Enter Password: ' password if [ "$password" == "1234" ]; then #Space around '[' is required echo -e "nSuccess!" else echo -e "nTry Again!" fi echo "" read -p "Enter numbers: " -a array echo ${array[2]}
This script will produce such output:
Enter your name: User1
Enter password:
Success #if password entered was 1234
Enter numbers: 100 200 300 400
300
Afterword
I hope this small handbook was interesting and helpful. To be honest, I wrote this handbook for myself so as to not forget the bash basics. I tried to write concisely but meaningfully, and I hope you will appreciate that.
This handbook narrates my own experience with Bash. It does not purport to be comprehensive, so if you still want more, please run man bash and start there.
Contributions are absolutely welcome and I will be grateful for any corrections or questions you can send my way. For all of that create a new issue.
Thanks for reading this handbook!
Want to learn more?
Here’s a list of other literature covering Bash:
- Bash man page. In many environments that you can run Bash, the help system
mancan display information about Bash, by running the commandman bash. For more information on themancommand, see the web page «The man Command» hosted at The Linux Information Project. - «Bourne-Again SHell manual» in many formats, including HTML, Info, TeX, PDF, and Texinfo. Hosted at https://www.gnu.org/. As of 2016/01, this covers version 4.3, last updated 2015/02/02.
- «Bash Shell Script» to learn how to write scripts using bash. It is hosted at https://www.gnu.org/.
Other resources
- awesome-bash is a curated list of Bash scripts and resources
- awesome-shell is another curated list of shell resources
- bash-it provides a solid framework for using, developing and maintaining shell scripts and custom commands for your daily work.
- Bash Guide for Beginners a good resource between the HOWTO and the Bash Scripting guide.
- dotfiles.github.io is a good source of pointers to the various dotfiles collections and shell frameworks available for bash and other shells.
- learnyoubash helps you write your first bash script
- shellcheck is a static analysis tool for shell scripts. You can either use it from a web page at www.shellcheck.net or run it from the command line. Installation instructions are on the koalaman/shellcheck github repository page.
Finally, Stack Overflow has many questions that are tagged as bash that you can learn from and is a good place to ask if you’re stuck.
License
© Denys Dovhan
Время на прочтение
14 мин
Количество просмотров 96K
Всем привет. Это перевод из книги по подготовке к экзамену RedHat RHCE. На мой взгляд очень доступно рассказывается об основах bash.
Сценарии оболочки — наука сама по себе. Не вдаваясь в подробности всего, что происходит «под капотом», вы узнаете, как применять базовые элементы для написания собственных скриптов, и анализировать, что происходит в сторонних сценариях оболочки.

Понимание основных элементов сценариев оболочки
По сути, сценарий оболочки представляет собой список команд, которые выполняются последовательно, а также некоторую логику, позволяющую выполнять код только при определённых условиях.
Чтобы понять сложные сценарии оболочки, рекомендуется начать с базовых сценариев.
Ниже показан очень простой скрипт:
#!/bin/bash
#
# #This is a script that greets the world
# Usage: ./hello
clear
echo hello world
exit 0
Здесь содержатся несколько элементов, которые должны использоваться во всех скриптах. Для начала, есть shebang — это строка #!/bin/bash. Когда скрипт запускается из родительской оболочки, он открывает подоболочку, в которой и выполняются команды, указанные в скрипте.
Эти команды могут быть интерпретированы различными способами. Для того, чтобы понять, как именно они должны интерпретироваться, используется shebang. В примере выше shebang ясно даёт понять, что скрипт должен выполняться оболочкой bash.
Также могут быть указаны другие оболочки. Например, если ваш скрипт содержит код на языке Perl, shebang должен быть #!/usr/bin/perl. Начинать сценарий с shebang является хорошей практикой; если он опущен, код сценария будет выполняться такой же оболочкой, которая используется для запуска скрипта.
Сразу после shebang расположена часть, объясняющая, о чем сценарий. Несколько строк комментариев в начале каждого сценария — хорошая идея. В коротком скрипте часто очевидно, что он делает, но когда сценарий становится длиннее, и по мере того, как всё больше людей вовлекаются в его написание и поддержку, становится менее понятно, что авторы намереваются сделать.
Чтобы избежать такой ситуации, убедитесь, что вы добавили строки комментариев, начиная каждую символом #. Комментарии могут быть не только в первых строках, но и в начале каждого подраздела сценария. Это наверняка поможет, если вы прочитаете свой скрипт через несколько месяцев!
Вы также можете комментировать не только подразделы, но и отдельные строки.
Независимо от того, в какой позиции он используется, всё от символа # и до конца строки является комментарием.
После блока комментариев расположено тело сценария. В вышеуказанном примере это несколько команд, выполняющихся последовательно. Тело сценария оболочки может увеличиваться по мере его развития.
В конце скрипта я включил инструкцию exit 0. Оператор выхода сообщает родительской оболочке, был ли сценарий успешным. Состояние выхода последней команды в сценарии является состоянием выхода самого сценария, если только команда exit 0 не используется в конце сценария.
Полезно знать, что вы можете работать с exit, чтобы сообщить родительской оболочке, как все прошло.
Введённая в родительской оболочке, команда echo $? позволяет запросить состояние выхода последнего запущенного сценария.
После создания сценария убедитесь, что он может быть выполнен. Самый распространённый способ сделать это — применить к нему бит выполнения. Итак, если имя файла скрипта — hello, используйте команду chmod +x ./hello для того, чтобы сделать его исполняемым.
Сценарий также может быть выполнен в качестве аргумента команды bash. В этом случае введите bash ./hello для запуска скрипта hello. Если сценарий запускается как аргумент команды bash, файл скрипта не обязательно должен быть исполняемым.
По сути, вы можете хранить скрипт в любом месте, но если вы собираетесь хранить его в каталоге, который не включён в переменную $PATH, вам нужно выполнить его с ./ перед именем скрипта.
Введите ./hello для того, чтобы запустить сценарий, или поместите его в стандартный каталог, который включён в переменную $PATH, например, /usr/local/bin.
Также вы можете разместить сценарий в каталоге /bin, после чего в любом месте файловой системы просто ввести имя файла, и сценарий выполнится.
Пример
Командой vi /bin/datetime создадим в каталоге /bin файл с именем datetime. В созданный файл вставим это содержимое:
#!/bin/bash
# выводит дату и время. а так же список пользователей, которые залогинены в данный момент
date
who
Сохранив файл, введите chmod +x /bin/datetime, чтобы дать файлу права на выполнение. Перейдите, к примеру, в домашний каталог с помощью команды cd ~ и просто введите datetime.
Перейдите, к примеру, в домашний каталог cd ~ и просто введите datetime.
[root@localhost ~]# datetime
Sat Sep 28 00:33:41 EDT 2019
root tty1 2019-09-25 20:28
root pts/0 2019-09-27 20:07 (comp.corp.domain.ru)
Использование переменных и входных данных
bash-скрипты — это гораздо больше, чем просто список команд, которые выполняются последовательно. Одна из приятных сторон скриптов заключается в том, что они могут работать с переменными и входными данными, чтобы сделать скрипт гибким. В этом разделе вы узнаете, как с ними работать.
Использование позиционных параметров
При запуске скрипта можно использовать аргументы. Аргумент — это всё, что вы помещаете за командой сценария. Аргументы могут быть использованы для того, чтобы сделать скрипт более гибким. Возьмём команду useradd lisa. В этом примере команда — это useradd, а её аргумент — lisa — указывает, что нужно сделать.
В результате выполнения такой команды должен быть создан пользователь с именем lisa.
В тексте сценария первый аргумент обозначается $1, второй аргумент — $2 и т. д. Листинг 1 показывает, как можно использовать аргументы. Попробуйте запустить этот код, указав в качестве параметров любые имена пользователей.
Листинг 1
#!/bin/bash
# run this script with a few arguments
echo The first argument is $1
echo The second argument is $2
echo The third argument is $3
Под параметрами подразумевается ввод данных перед запуском скрипта. В данном случае в качестве параметров после имени скрипта argument я указал lisa, lori и bob:
[root@server1 ~]# ./argument lisa lori bob
The first argument is lisa
The second argument is lori
The third argument is bob
[root@server1 ~]#
Если вы попытались запустить пример кода, то могли заметить, что его содержимое не идеально. Если вы используете три аргумента при выполнении скрипта из листинга 1, он будет работать отлично. Если используете только два аргумента, третий выводится без значения $3.
Если же используете четыре аргумента, четвёртое значение (которое будет храниться в $4) никогда не отобразится. Итак, если вы хотите использовать аргументы, вам лучше использовать более гибкий подход.
Листинг 2
#!/bin/bash
# run this script with a few arguments
echo you have entered $# arguments
for i in $@
do
echo $i
done
exit 0
В Листинге 2 представлены два новых элемента, которые относятся к аргументам:
- $# — это счетчик, который показывает, сколько аргументов было использовано при запуске скрипта.
- $@ — список всех аргументов, которые использовались при запуске скрипта.
Чтобы перечислить аргументы, которые использовались при запуске этого скрипта, используется цикл for. В циклах for команды выполняются до тех пор, пока условие истинно. В этом сценарии условие for i in $@ означает «для каждого аргумента». Каждый раз, когда сценарий проходит цикл, значение из переменной $@ присваивается переменной $i.
Итак, пока есть аргументы, тело сценария выполняется.
Тело цикла for всегда начинается с do и закрывается done, а между этими двумя ключевыми словами перечисляются команды, которые необходимо выполнить. Таким образом, пример сценария будет использовать echo для отображения значения каждого аргумента и останавливаться, когда больше нет доступных аргументов.
Давайте попробуем воспользоваться скриптом из листинга 2 в этом примере:
- Введите vi argument, чтобы создать файл argument и скопируйте содержимое из скрипта листинга 2 в этот файл.
- Сохраните файл и сделайте его исполняемым.
- Запустите команду ./argument a b c. Вы увидите, что отобразятся три строки.
- Запустите команду ./argument a b c d e f. Вы увидите, что помимо a b c отобразятся и d e f.
Переменные
Переменная — это метка, которая используется для обозначения определённого места в памяти, которое содержит определённое значение. Переменные могут быть определены статически с помощью NAME=value или динамическим способом. Существует два решения для динамического определения переменной:
- Используйте ключевое слово read в скрипте, чтобы запросить данные у пользователя, запускающего скрипт.
- Используйте подстановку команд, чтобы использовать результат команды и назначить его переменной. Например, команда date +%d-%m-%y показывает текущую дату в формате день-месяц-год. Чтобы сделать это в сценарии, вы можете использовать TODAY=$(date +%d-%m-%y). Для подстановки команд вам просто нужно поместить команду, результат которой вы хотите использовать, между скобками.
В предыдущем разделе о позиционных параметрах вы узнали, как присваивать аргументы переменным при запуске скрипта. В некоторых случаях может быть более эффективно запрашивать информацию, когда вы обнаружите, что чего-то существенного не хватает. Сценарий ниже показывает, как это сделать.
Листинг 3. Пример скрипта, использующего команду read
#!/bin/bash
if [ -z $1 ]; then
echo enter a text
read TEXT
else
TEXT=$1
fi
echo you have entered the text $TEXT
exit 0
В сценарии листинга 3 оператор if… then… else… fi используется для проверки существования аргумента $1. Это делается с помощью test (test — это отдельная команда). Команда test может быть написана двумя способами*: test или [… ]. В примере строка if [ -z $1 ] … выполняется, чтобы увидеть тест (проверку) -z $1.
* — на самом деле тремя источник (прим. переводчика)
-z test проверяет, существует или нет $1. Иначе говоря, строка if [ -z $1 ] проверяет, является ли $1 пустым, что означает, что при запуске этого сценария не было предоставлено никаких аргументов. Если это так, команды после оператора then выполняются.
Обратите внимание, что при написании команды test с квадратными скобками важно использовать пробелы после открывающей скобки и перед закрывающей скобкой, без пробелов команда не будет работать.
Обратите внимание, что оператор then следует сразу за test. Это возможно, потому что используется точка с запятой (;). Точка с запятой является разделителем команд и может заменить новую строку в скрипте.
В операторе then выполняются две команды: команда echo, которая отображает сообщение на экране, и команда read.
Команда read останавливает сценарий, чтобы пользовательский ввод мог быть обработан и сохранен в переменной TEXT. Поэтому read TEXT помещает все введённые пользователем данные в переменную TEXT, которая будет использоваться позже в скрипте.
Следующая часть представлена оператором else. Команды после оператора else выполняются во всех других случаях, что в данном случае означает «иначе, если аргумент был предоставлен». Если это так, то определяется переменная TEXT и ей присваивается текущее значение $1.
Обратите внимание, как определяется переменная: непосредственно после имени переменной стоит знак =, за которым следует $1. Обратите внимание, что вы никогда не должны использовать пробелы при определении переменных.
Затем условия if замыкается с помощью оператора fi. После завершения условия if вы точно знаете, что переменная TEXT определена и имеет значение. Предпоследняя строка скрипта считывает значение переменной TEXT и отображает это значение в STDOUT с помощью команды echo. Обратите внимание, что для запроса текущего значения переменной ссылается на имя переменной, начиная со знака $ перед ним.
Вы можете попрактиковаться на этом примере при работе с вводом.
- Откройте редактор и создайте файл с именем text. Введите содержимое кода из листинга 3 в этот файл.
- Запишите файл на диск и выполните chmod +x text, чтобы сделать его исполняемым.
- Запустите скрипт, выполнив ./text и без дополнительных аргументов. Вы увидите, что он запрашивает ввод.
- Запустите скрипт, используя «hello» в качестве аргумента (./text hello). Результат отобразит «you have entered the text hello» в STDOUT.
Использование условий и циклов
Как вы уже видели, в скрипте могут использоваться условные операторы. Эти условные операторы выполняются только в том случае, если определённое условие выполняется.
В bash есть несколько условных операторов и циклов, которые часто используются.
- if… then… else — используется для выполнения кода, если определенное условие выполняется
- for — используется для выполнения команд для диапазона значений
- while — используется для выполнения кода, если выполняется определенное условие
- before — используется для выполнения кода, пока не выполнено определенное условие
- case — используется для оценки ограниченного количества конкретных значений
if then else
Конструкция if then else является общей для оценки конкретных условий. Вы уже видели пример с ним. Этот условный оператор часто используется вместе с командой test. Эта команда позволяет вам проверять многие вещи: например, не только, существует ли файл, но и сравнивать файлы, сравнивать целые числа и многое другое.
Подробнее о test можно узнать в справочнике командой man test.
Основная конструкция if есть if… then… fi.
Она сравнивает одно условие, как показано в следующем примере:
if [ -z $1 ]
then
echo no value provided
fi
В листинге 3 вы увидели, как можно оценить два условия, включая else в выражении. В листинге 4 показано, как можно оценить несколько условий от if до else. Это полезно, если нужно проверить много разных значений.
Обратите внимание, что в этом примере также используются несколько команд test.
Листинг 4. Пример с if then else
#!/bin/bash
# run this script with one argument
# the goal is to find out if the argument is a file or a directory
if [ -f $1 ]
then
echo "$1 is a file"
elif [ -d $1 ]
then
echo "$1 is a directory"
else
echo "I do not know what $1 is"
fi
exit 0
|| и &&
Вместо написания полных операторов if… then вы можете использовать логические операторы || а также &&. || является логическим «ИЛИ» и выполнит вторую часть оператора, только если первая часть не верна; && является логическим «И» и выполнит вторую часть оператора только в том случае, если первая часть верна.
Рассмотрим эти две строки:
[ -z $1 ] && echo no argument provided
ping -c 1 8.8.8.8 2>/dev/null || echo node is not available
В первом примере выполняется проверка, чтобы увидеть, пуст ли $1. Если эта проверка верна (что, в основном, означает, что команда завершается с кодом выхода 0), выполняется вторая команда.
Во втором примере команда ping используется для проверки доступности хоста.
В этом примере используется логическое «ИЛИ» для вывода текста «node is not available» в случае неудачной команды ping.
Вы обнаружите, что часто вместо условного оператора if будут использоваться && и ||. В упражнении ниже вы можете попрактиковаться в использовании условных операторов, используя либо if… then… else, либо && и ||.
Упражнение. Использование if… then… else
В этом упражнении вы поработаете над сценарием, который проверяет что является файлом, а что каталогом.
- Запустите редактор и создайте скрипт с именем filechk.
- Скопируйте содержимое из листинга 4 в этот скрипт.
- Запустите с ним пару тестов, такие как ./filechk /etc/hosts, ./filechck /usr, ./filechk non-existing-file.
Цикл for
Цикл for представляет собой отличное решение для обработки диапазонов данных. В листинге 5 вы можете увидеть первый пример с for, где диапазон определяется и обрабатывается, пока в этом диапазоне есть необработанные значения.
Листинг 5
#!/bin/bash
#
for (( COUNTER=100; COUNTER>1; COUNTER-- )); do
echo $COUNTER
done
exit 0
Цикл for всегда начинается с ключевого слова for, за которым следует условие, которое необходимо проверить. Затем следует ключевое слово do, за которым следуют команды, которые должны быть выполнены, если условие истинно, завершается цикл с помощью ключевого слова done.
В примере, приведённом в листинге 5, вы можете увидеть, что условие представляет собой диапазон чисел в круглых скобках, назначенных переменной COUNTER.
Маленькое пояснение
Внутри ((… )) вычисляются арифметические выражения и возвращается их результат. Например, в простейшем случае, конструкция a=$(( 5 + 3 )) присвоит переменной «a» значение выражения «5 + 3», или 8. Кроме того, двойные круглые скобки позволяют работать с переменными в стиле языка C.
Сначала переменная инициализируется значением 100, и до тех пор, пока значение больше 1, в каждой итерации вычитается 1. Пока условие истинно, значение переменной $COUNTER отображается с использованием команды echo.
В листинге 6 вы можете увидеть один из моих любимых однострочников с for. Диапазон определяется на этот раз как последовательность чисел, начиная со 100 и доходя до 104.
Листинг 6
for i in {100..104}; do ping –c 1 192.168.4.$i >/dev/null && echo 192.168.4.$i is up; done
Обратите внимание, как определяется диапазон: сначала вы указываете первое число, затем две точки и указываете последнее число в диапазоне. При этом с for i in для каждого из этих номеров присваивается переменная i. Каждое из этих чисел присваивается переменной i и затем выполняется команда ping, где опция -c 1 гарантирует, что отправляется только один запрос.
Результат выполнения команды ping не учитывается, поэтому её вывод перенаправляется в /dev/null. На основании состояния выхода команды ping выполняется часть выражения за &&. Таким образом, если хост доступен, отображается строка, указывающая, что он работает.
Понимание while и until
Если оператор for, о котором вы только что прочитали, полезен для работы с диапазонами элементов, то оператор while полезен тогда, когда вы хотите отслеживать что-то вроде доступности процесса. Также существует оператор until, который выполняется до тех пор, пока проверяемое условие ложно. В листинге 7 вы можете прочитать, как while используется для мониторинга активности процесса.
Примечание. Так и не понял, что делает этот скрипт. В моём случае используется CentOS 7 и по умолчанию там нет monitor, хотя в скрипте явно написано:
usage: monitor <processname>Где-то пол часа гуглил для CetOS программу monitor, но так и не нашёл. И вообще не понятно каким тут боком monitor если используется ps aux. В любом случае так и не понял, что делает этот срипт. Большая просьба помочь решить этот вопрос, чтобы откорректировать текст и/или скрипт.
Листинг 7
#!/bin/bash
#
# usage: monitor <processname>
while ps aux | grep $1 | grep -v grep > /dev/tty11
do
sleep 5
done
clear
echo your process has stopped
logger $1 is no longer present
mail -s "process $1 has stopped" root < .
Сценарий в листинге 7 состоит из двух частей. Во-первых, есть цикл while. Во-вторых, есть всё, что нужно выполнить, когда цикл while больше не оценивается как true.
Ядром цикла while является команда ps, которая имеет значение $1.
Обратите внимание на использование grep -v grep, который исключает строки, содержащие команду grep, из результата. Помните, что команда ps будет включать в себя все запущенные процессы, включая команду grep, которой передаётся вывод команды ps. Это может привести к ложному положительному совпадению.
Вывод команды ps aux перенаправляются в /dev/tty11. Это позволяет позже прочитать результаты из tty11, если это необходимо, но они не отображаются по умолчанию.
После операторов while следуют команды, которые необходимо выполнить, если проверяемое условие истинно. В данном случае это команда sleep 5, которая приостанавливает выполнение скрипта на 5 секунд.
Пока условие оператора while истинно, цикл продолжает выполняться. Если же условие ложно (что в данном случае означает, что процесс больше не доступен), то цикл останавливается и могут выполняться команды, следующие за ним.
Вам должны быть знакомы все эти команды, кроме последней. В строке mail -s “process $1 has stopped” root < . сообщение отправляется пользователю root с использованием внутренней почтовой системы, которая по умолчанию работает на Linux*. Команда mail принимает в качестве первого аргумента тему сообщения, указанную с помощью опции -s.
* — по крайней мере в CentOS по умолчанию работает. (при. переводчика)
Обратите внимание на < . в конце команды.
Обычно при использовании команды mail в интерактивном режиме открывается редактор, в котором можно написать тело сообщения. Этот редактор закрыт, предоставляя строку, которая имеет только точку. В этой команде точка предоставляется путём перенаправления STDIN. Это позволяет обрабатывать сообщение без каких-либо дополнительных требований к пользовательской активности.
Цикл while — это противоположность циклу until, пример которого приведён в листинге 8. until запускает итерацию, которая длится до тех пор, пока условие не станет истинным.
В листинге 8 он используется для фильтрации выходных данных команды users по вхождению $1, которое будет именем пользователя. Пока эта команда не верна, итерация продолжается. Когда имя пользователя найдено в выводе пользователей, итерация закрывается, и после цикла until выполняются остальные команды.
Листинг 8
#!/bin/bash
#
until users | grep $1 > /dev/null
do
echo $1 is not logged in yet
sleep 5
done
echo $1 has just logged in
mail -s "$1 has just logged in" root < .
Понимание case
Последний из важных итерационных циклов — это case*. Оператор case используется для оценки ряда ожидаемых значений. В частности, инструкции case важны в сценариях запуска Linux, которые в предыдущих версиях использовались для запуска служб.
* — цикл ли?
В операторе case вы определяете каждый ожидаемый вами конкретный аргумент, за которым следует команда, которую необходимо выполнить, если этот аргумент использовался.
В листинге 9 вы можете увидеть работу оператора case, который использовался в ранней версии для запуска практически любой службы.
Листинг 9
case "$1" in
start)
start;;
stop)
rm -f $lockfile
stop;;
restart)
restart;;
reload)
reload;;
status)
status
;;
*)
echo "Usage: $0 (start|stop|restart|reload|status)"
;;
esac
case имеет несколько особенностей. Сначала идет строка — case последовательность in. Затем следует список всех возможных значений, которые необходимо оценить. Каждый элемент закрывается скобкой ).
Затем следует список команд, которые необходимо выполнить, если использовался конкретный аргумент. Список команд закрывается двойной точкой с запятой ;; может использоваться непосредственно после последней команды и может использоваться в отдельной строке.
Также обратите внимание, что *) относится ко всем другим параметрам, не указанным ранее. Это «всеохватывающий» оператор.
Цикл итераций case завершается оператором esac.
Обратите внимание, что последовательности в case выполняются по порядку. Когда будет выполнено первое совпадение, оператор case не будет ничего оценивать.
В рамках оценки могут использоваться шаблоны, подобные шаблону. Это показано в *) последовательности, которая соответствует всему. Но вы также можете использовать последовательности, такие как start|Start|START), чтобы соответствовать использованию другого case.
Отладка скриптов в Bash
Когда скрипт не делает того, что вы ожидаете, полезно выполнить некоторую отладку. Для начала, попробуйте выполнить его как аргумент команды bash -x. Это покажет вам построчно, что пытается сделать скрипт, а также конкретные ошибки, если он не работает как надо.
В листинге 10 показан пример использования bash -x, где сразу становится ясно, что команда grep не знает, что она должна делать, так как пропущен аргумент для её работы.
[root@server1 ~]# bash -x 319.sh
+ grep
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.
+ users
+ echo is not logged in yet is not logged in yet
+ sleep 5
Подводим итоги
В этой статье вы узнали, как писать сценарии оболочки. Вы рассмотрели несколько примеров и теперь знакомы с некоторыми основными элементами, необходимыми для создания успешного сценария.
