0
0
Bash Scriptingscripting~10 mins

Writing to files (echo, printf) in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to write the text 'Hello World' into a file named output.txt using echo.

Bash Scripting
echo "Hello World" [1] output.txt
Drag options to blanks, or click blank then click option'
A>>
B>
C<
D|
Attempts:
3 left
💡 Hint
Common Mistakes
Using >> appends instead of overwriting.
Using < which is for input redirection.
Using | which pipes output to another command.
2fill in blank
medium

Complete the code to append the text 'New Line' to the file log.txt using echo.

Bash Scripting
echo "New Line" [1] log.txt
Drag options to blanks, or click blank then click option'
A>>
B>
C<
D|
Attempts:
3 left
💡 Hint
Common Mistakes
Using > which overwrites the file.
Using < which is for input redirection.
Using | which pipes output to another command.
3fill in blank
hard

Fix the error in the code to write formatted text with printf into file.txt.

Bash Scripting
printf "Name: %s\n" [1] > file.txt
Drag options to blanks, or click blank then click option'
AJohn
B'John'
C"John"
DName
Attempts:
3 left
💡 Hint
Common Mistakes
Not quoting the string, causing errors or unexpected output.
Using single quotes which may not expand variables if used.
Passing a bare word that is not defined.
4fill in blank
hard

Fill both blanks to create a file with numbers 1 to 5, each on a new line, using printf and a loop.

Bash Scripting
for i in {1..5}; do printf "[1]\n" [2]; done > numbers.txt
Drag options to blanks, or click blank then click option'
A%d
Bi
C$i
D%s
Attempts:
3 left
💡 Hint
Common Mistakes
Using i without $ which is treated as a string.
Using %s which is for strings, not numbers.
Not redirecting output to the file.
5fill in blank
hard

Fill all three blanks to create a file with squares of numbers 1 to 3 using printf and arithmetic expansion.

Bash Scripting
for n in {1..3}; do printf "[1]: [2]\n" [3] $((n*n)); done > squares.txt
Drag options to blanks, or click blank then click option'
ANumber
BSquare
Cn
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same label twice.
Using incorrect variable names.
Not using arithmetic expansion correctly.