Create and Use Temporary Files with mktemp in Bash
📖 Scenario: You are writing a bash script that needs to store some temporary data safely without overwriting existing files. Using temporary files helps keep your system clean and avoids conflicts.
🎯 Goal: Build a bash script that creates a temporary file using mktemp, writes some text into it, and then displays the content of the temporary file.
📋 What You'll Learn
Create a temporary file using mktemp
Write the exact text Hello, temporary file! into the temporary file
Display the content of the temporary file using cat
💡 Why This Matters
🌍 Real World
Temporary files are used in scripts to store data temporarily without risking overwriting important files. This is common in automation, backups, and data processing.
💼 Career
Knowing how to safely create and manage temporary files is essential for system administrators, DevOps engineers, and anyone writing reliable bash scripts.
Progress0 / 4 steps
1
Create a temporary file with mktemp
Write a line of bash code to create a temporary file using mktemp and save its path in a variable called tempfile.
Bash Scripting
Hint
Use tempfile=$(mktemp) to create a temporary file and store its name.
2
Write text into the temporary file
Add a line of bash code to write the exact text Hello, temporary file! into the file stored in the variable tempfile.
Bash Scripting
Hint
Use echo "Hello, temporary file!" > "$tempfile" to write text into the temporary file.
3
Display the content of the temporary file
Add a line of bash code to display the content of the temporary file stored in tempfile using cat.
Bash Scripting
Hint
Use cat "$tempfile" to show the file content.
4
Print the temporary file path
Add a line of bash code to print the path of the temporary file stored in tempfile using echo.
Bash Scripting
Hint
Use echo "Temporary file path: $tempfile" to show the file path.
Practice
(1/5)
1. What is the main purpose of the mktemp command in bash scripting?
easy
A. To permanently delete files from the system
B. To change file permissions
C. To list all files in the current directory
D. To create a unique temporary file or directory safely
Solution
Step 1: Understand the function of mktemp
mktemp is designed to create temporary files or directories with unique names to avoid conflicts.
Step 2: Compare with other options
Options A, C, and D describe unrelated commands or actions not related to mktemp.
Final Answer:
To create a unique temporary file or directory safely -> Option D
2. Which of the following is the correct syntax to create a temporary file using mktemp in bash?
easy
A. mktemp
B. mktemp -d
C. mktemp --remove
D. mktemp -r
Solution
Step 1: Identify basic mktemp usage
Running mktemp alone creates a temporary file with a unique name.
Step 2: Understand options
-d creates a temporary directory, not a file. Options --remove and -r are invalid or not standard.
Final Answer:
mktemp -> Option A
Quick Check:
Basic mktemp = create temp file [OK]
Hint: Use plain mktemp to create a temp file quickly [OK]
Common Mistakes:
Using -d when a file is needed, creates directory instead
Using non-existent options like --remove or -r
Confusing directory creation with file creation
3. What will be the output of the following bash script snippet?
tempfile=$(mktemp)
echo $tempfile
rm $tempfile
medium
A. Creates a directory instead of a file
B. Prints a unique temporary filename, then deletes it
C. Prints the string 'tempfile' literally
D. Prints an error because tempfile is empty
Solution
Step 1: Analyze mktemp usage
mktemp creates a unique temporary file and returns its path, stored in tempfile.
Step 2: Understand echo and rm commands
echo $tempfile prints the temporary filename. rm $tempfile deletes the file to keep the system clean.
Final Answer:
Prints a unique temporary filename, then deletes it -> Option B
Quick Check:
mktemp creates file, echo prints, rm deletes [OK]
Hint: mktemp returns filename; echo prints it; rm cleans up [OK]
Common Mistakes:
Expecting literal 'tempfile' output instead of variable value
Thinking tempfile is empty or unset
Confusing file creation with directory creation
4. Identify the error in this script snippet:
tempfile=mktemp
echo $tempfile
rm $tempfile
medium
A. Missing parentheses to execute mktemp command
B. rm command should be before echo
C. mktemp should be quoted
D. No error, script works fine
Solution
Step 1: Check variable assignment
Assigning tempfile=mktemp sets the string 'mktemp', not the command output.
Step 2: Correct command substitution
Use tempfile=$(mktemp) to run the command and assign its output.
Final Answer:
Missing parentheses to execute mktemp command -> Option A
Quick Check:
Use $(mktemp) to assign command output [OK]
Hint: Use $(command) to assign output, not plain text [OK]
Common Mistakes:
Assigning command name as string instead of output
Forgetting $() or backticks for command substitution
Thinking rm order affects error here
5. You want to create a temporary directory for your script to store files safely and remove it after use. Which command correctly creates this directory and stores its path in a variable named tempdir?
hard
A. tempdir=$(mktemp)
B. tempdir=mktemp -d
C. tempdir=$(mktemp -d)
D. tempdir=mktemp -f
Solution
Step 1: Understand -d option in mktemp
The -d option tells mktemp to create a temporary directory instead of a file.
Step 2: Correct command substitution syntax
Use $(mktemp -d) to execute the command and assign the directory path to tempdir.
Final Answer:
tempdir=$(mktemp -d) -> Option C
Quick Check:
Use $(mktemp -d) for temp directory creation [OK]
Hint: Use $(mktemp -d) to create and assign temp directory [OK]
Common Mistakes:
Assigning command as string without $()
Using mktemp without -d creates a file, not directory
Using invalid options like -f
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.