Variables are named values
useful for storing data or command output
設定變數值:variable=value
取出變數傎:echo $value
如:
[root@localhost ~]# john=$(ls /etc/)
[root@localhost ~]# echo $john
a2ps.cfg a2ps-site.cfg acpi adjtime aliases aliases.db alsa alternatives anacrontab asound.state at.deny audit autofs_ldap_auth.conf auto.master auto.misc auto.net auto.smb avahi bashrc blkid bluetooth bonobo-activation capi.conf cdrecord.conf conman.conf cron.d cron.daily cron.deny cron.hourly cron.monthly crontab cron.weekly csh.cshrc csh.login cups dbus-1 default depmod.d desktop-profiles dev.d DIR_COLORS DIR_COLORS.xterm
...skip....
[root@localhost ~]#
[root@localhost ~]# hi="hello,nice to meet you."
[root@localhost ~]# echo $hi
hello,nice to meet you.
[root@localhost ~]#
Configuration variables
PS1:man 1 bash
\h | short hostname |
\u | user name |
\w | the current working directory |
\! | the history number of the current command |
\$ | shows $ |
PATH: 可執行程式的路徑
EDITOR: 預設文字編輯器
HISTFILESIZE: Number of commands in bash history
資訊變數:
HOME:使用者目錄
EUID:使用者有效的UID
[root@localhost ~]# man 1 bash
BASH(1) BASH(1)
NAME
bash - GNU Bourne-Again SHell
SYNOPSIS
bash [options] [file]
COPYRIGHT
Bash is Copyright (C) 1989-2005 by the Free Software Foundation, Inc.
DESCRIPTION
Bash is an sh-compatible command language interpreter that executes commands read from the
standard input or from a file. Bash also incorporates useful features from the Korn and C
shells (ksh and csh).
Bash is intended to be a conformant implementation of the IEEE POSIX Shell and Tools speci-
fication (IEEE Working Group 1003.2). Bash can be configured to be POSIX-conformant by
default.
OPTIONS
In addition to the single-character shell options documented in the description of the set
builtin command, bash interprets the following options when it is invoked:
-c string If the -c option is present, then commands are read from string. If there are
arguments after the string, they are assigned to the positional parameters, start-
ing with $0.
-i If the -i option is present, the shell is interactive.
-l Make bash act as if it had been invoked as a login shell (see INVOCATION below).
Use aliases let you create shortcuts to command
使用別名可以讓使用者快速和便倢的使用指令
如:
將 ls -la 取別名為dir
[root@localhost ~]# alias dir='ls -la'
此時只要執行 dir 即是執行 "ls -la"
Use alias by itself to see all set aliases
[root@localhost ~]#alias dir
alias dir='ls -la'
bash Expands a command line
alias 能夠和其他指令混用
curly-brace statements ({})
tilde statements (~)
variables ($) 通常用 $ ${} $(()) $[] $() `
Expand file globs (*, ?, [abc], etc)
Prepare I/O redirections (<, >)
Run the command !
Quoting(\) prevents expansion
- Single quotes (') inhibit all expansion
- Double quotes (") inhibit all expansion, except:
- $ (dollar sign) - variable expansion
- ` (backquotes) - command substitution
- \ (backslash) - single character inhibition
- ! (exclamation point) - history substitution
Bash 執行任務:profile
bash執行過程會依序載入這些啟動命令
儲存在 /etc/profile and ~/.bash_profile
只有可以登入的 shell 才有作用
通常用來設定環境變數
Running commands (eg. mail-checker script)
儲存在 /etc/bashrc and ~/bashrc
Run for all shells
通常用來Setting 本地端的變數
Defining aliases(別名設定)
Bash 離開時的任務
儲存在 ~/.bash_logout (user)
在 login shell 離開時執行
Creating automatic backups
Cleaning out temporary files
取得 input with the read 指令
使用 read to assign input 值 to one or more shell 變數:
-p designates prompt to display
read reads from standard input and assigns one word to each variable
Any leftover words are assigned to the last variable
read -p "Enter a filename: " FILE