String and Arithmetic Expansion
Part of the Bash course
You will constantly find yourself with a variable that holds almost, but not quite, what you need. A full file path when you only want the filename. A filename when you only want the extension. A sentence where one word needs swapping out. A pair of numbers you need added together for a report.
Imagine a price tag that reads $19.99 and you want just the number, 19.99, with the dollar sign trimmed off the front. Or a photo file called vacation.jpg where you want just vacation, with the .jpg trimmed off the back. Bash has a piece of syntax for exactly this kind of trimming, and it does not need any external tool. ...
Want to learn Bash String and Arithmetic Expansion in a real environment?
Click below, get a fresh Linux box with Bash ready to go, and work through String and Arithmetic Expansion hands-on with an AI tutor that knows your code.
Start this lessonString and Arithmetic ExpansionWhat you'll learn
- ${var#prefix}
- ${var%suffix}
- ${var//old/new}
- $(( arith ))
- Printf formatting
Read this lesson in your language
All lessons in this course
12 lessons- 1What Is Bash20 min
- 2Shebang and Your First Script25 min
- 3Variables and Quoting25 min
- 4Conditionals and Tests30 min
- 5Loops25 min
- 6Functions25 min
- 7Arrays25 min
- 8String and Arithmetic Expansion25 min
- 9IO Redirection and Pipes25 min
- 10Error Handling and Traps30 min
- 11Debugging and Best Practices25 min
- 12Capstone: Server Bootstrap Script60 min
