Bash: expansion

#!/bin/bash                                                                                                                                                                                                     
echo START
# brace expansion                                                                                                                                                                                               
echo {John,Betty,Robert}@windriver.com
# tidle expansion                                                                                                                                                                                               
echo MyDir=~
# command substitution                                                                                                                                                                                          
cwd=$(pwd)
# straight parameter expansion                                                                                                                                                                                  
echo ${cwd}
# indirect parameter expansion                                                                                                                                                                                  
var=foo
foo=bar
bar=another
echo ${var}                     # print foo                                                                                                                                                                     
echo ${!var}                    # print bar                                                                                                                                                                     
# arithmetic expansion                                                                                                                                                                                          
var=1
echo $(($var+1))                # print 2                                                                                                                                                                       
# parameter expansion with default value                                                                                                                                                                        
echo ${var:-default}
echo ${uvar:-default}           # print default                                                                                                                                                                 
echo ${uvar}                    # print nothing                                                                                                                                                                 
# parameter expansion with defaul value assigning                                                                                                                                                               
echo ${uvar:=default}
echo ${uvar}
# substring expansion ${var:offset:length}                                                                                                                                                                      
echo ${cwd:4}
echo ${cwd:4:5}
# names mathcing prefix                                                                                                                                                                                         
echo ${!v*}                     # output var                                                                                                                                                                    
# parameter length                                                                                                                                                                                              
echo ${#*}
echo ${#cwd}
# remove matching prefix pattern                                                                                                                                                                                
fake_base_dir=${cwd#/*/}        # shortest matching pattern                                                                                                                                                     
echo ${fake_base_dir}
base_dir=${cwd##/*/}            # longgest matching pattern                                                                                                                                                     
echo ${base_dir}
# remove matching suffix pattern                                                                                                                                                                                
parent_dir=${cwd%/*}
echo ${parent_dir}
empty_dir=${cwd%%/*}
echo ${empty_dir}
# pattern substitution ${para/pattern/string}                                                                                                                                                                   
echo ${cwd/\/*\//SUBSTRING}
echo ${cwd/\/*\//}              # same effect with ${cwd##/*/}                                                                                                                                                  
# case modification                                                                                                                                                                                             
var='HELLO WORLD hello world'
echo ${var^[hello]}
echo ${var^^[hello]}
echo ${var,[HELLO]}
echo ${var,,[HELLO]}
echo ${var^^}
echo ${var,,}
echo END

# for more detailed usage, refer to bash manual

你可能感兴趣的:(parameter,command,bash,expansion)