$test
값을 보유 하는 변수가 있다고 가정 해 봅시다. asd 123
예를 들어 asd
부분 을 자르는 가장 간단한 방법은 무엇 입니까?
나는 이것을 잠시 봤는데 답을 찾을 수 없다는 것에 놀랐다.
에서 Bourne의 같은 등 껍질, dash
( /bin/sh
우분투 ), bash
그리고 ksh
거기로 알려진 뭔가 매개 변수 확장 , 그리고 POSIX 표준에 의해 지정하는 기능입니다. 구체적으로 dash
매뉴얼 을 인용하려면 :
${parameter%word} Remove Smallest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest
portion of the suffix matched by the pattern deleted.
${parameter%%word} Remove Largest Suffix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
portion of the suffix matched by the pattern deleted.
${parameter#word} Remove Smallest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the smallest
portion of the prefix matched by the pattern deleted.
${parameter##word} Remove Largest Prefix Pattern. The word is expanded to produce a pattern. The parameter expansion then results in parameter, with the largest
portion of the prefix matched by the pattern deleted.
따라서 asd
부품 을 교체 하려면 다음을 수행 할 수 있습니다.
$ var="asd 123"
$ echo ${var#asd*}
123
제거하려면 123
다음을 수행하십시오.
$ echo ${var%*123}
asd
bash
또 다른 형태로 더욱이와 함께 간다 : ${parameter/string/replacement}
와 ${parameter//string/replacement}
. 첫 번째 문자열은 첫 번째 문자열을 대체합니다. 두 번째 양식은 모든 발생을 대체합니다. 예를 들면 :
$ echo ${var//123/}
asd
$ echo ${var//asd/}
123
구문에 따라 replacement
부분은 빈 문자열입니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다