Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Temporary files (mktemp) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Temporary File Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this mktemp command?
Consider the following command run in a bash shell:

mktemp /tmp/tempfile.XXXXXX

What does this command output?
Bash Scripting
mktemp /tmp/tempfile.XXXXXX
AThe literal string '/tmp/tempfile.XXXXXX' without changes
BA unique temporary filename starting with /tmp/tempfile. followed by 6 random characters
CA directory named tempfile.XXXXXX created in /tmp
DAn error message because mktemp requires no arguments
Attempts:
2 left
💡 Hint
mktemp replaces the XXXXXX with random characters to create a unique filename.
💻 Command Output
intermediate
2:00remaining
What error does this mktemp command produce?
What error message will this command produce?

mktemp /tmp/tempfile

Note: The template does not contain any 'X' characters.
Bash Scripting
mktemp /tmp/tempfile
Amktemp: too few X's in template '/tmp/tempfile'
BNo error, creates a file named /tmp/tempfile
Cmktemp: cannot create temporary file
Dmktemp: invalid option -- 't'
Attempts:
2 left
💡 Hint
mktemp requires at least 3 'X' characters in the template to replace.
🔧 Debug
advanced
2:00remaining
Why does this mktemp command fail to create a file?
Given this command:

mktemp -d /tmp/mydir.XXXXXX

It fails with an error 'cannot create directory'. What is the most likely reason?
Bash Scripting
mktemp -d /tmp/mydir.XXXXXX
AThe -d option is invalid and causes failure
BThe template must not contain any X characters when using -d
CThe /tmp directory does not exist or is not writable
Dmktemp cannot create directories, only files
Attempts:
2 left
💡 Hint
Check if the directory where you want to create the temp directory exists and is writable.
🚀 Application
advanced
2:00remaining
Which script snippet safely creates and cleans up a temporary file?
You want to create a temporary file, write 'Hello' into it, then delete it after use. Which snippet does this correctly?
Atmpfile=$(mktemp) && echo 'Hello' > "$tmpfile" && cat "$tmpfile" && rm "$tmpfile"
Btmpfile=mktemp && echo 'Hello' > $tmpfile && cat $tmpfile && rm $tmpfile
Cmktemp > tmpfile && echo 'Hello' > tmpfile && cat tmpfile && rm tmpfile
Dtmpfile=$(mktemp) && echo 'Hello' > tmpfile && cat tmpfile && rm tmpfile
Attempts:
2 left
💡 Hint
Remember to capture the output of mktemp in a variable and quote variables when used.
🧠 Conceptual
expert
2:00remaining
Why is mktemp preferred over manual temp file naming?
Why should scripts use mktemp to create temporary files instead of manually naming files like /tmp/tempfile.txt?
AManual naming automatically cleans up files after use
BManual naming is faster and more secure than mktemp
Cmktemp creates files with fixed names for easier debugging
Dmktemp guarantees unique filenames and avoids race conditions
Attempts:
2 left
💡 Hint
Think about what happens if two scripts use the same temp filename.

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

  1. Step 1: Understand the function of mktemp

    mktemp is designed to create temporary files or directories with unique names to avoid conflicts.
  2. Step 2: Compare with other options

    Options A, C, and D describe unrelated commands or actions not related to mktemp.
  3. Final Answer:

    To create a unique temporary file or directory safely -> Option D
  4. Quick Check:

    mktemp = unique temp file creation [OK]
Hint: Remember: mktemp = safe unique temp file creation [OK]
Common Mistakes:
  • Confusing mktemp with file deletion commands
  • Thinking mktemp lists files
  • Assuming mktemp changes file permissions
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

  1. Step 1: Identify basic mktemp usage

    Running mktemp alone creates a temporary file with a unique name.
  2. Step 2: Understand options

    -d creates a temporary directory, not a file. Options --remove and -r are invalid or not standard.
  3. Final Answer:

    mktemp -> Option A
  4. 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

  1. Step 1: Analyze mktemp usage

    mktemp creates a unique temporary file and returns its path, stored in tempfile.
  2. Step 2: Understand echo and rm commands

    echo $tempfile prints the temporary filename. rm $tempfile deletes the file to keep the system clean.
  3. Final Answer:

    Prints a unique temporary filename, then deletes it -> Option B
  4. 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

  1. Step 1: Check variable assignment

    Assigning tempfile=mktemp sets the string 'mktemp', not the command output.
  2. Step 2: Correct command substitution

    Use tempfile=$(mktemp) to run the command and assign its output.
  3. Final Answer:

    Missing parentheses to execute mktemp command -> Option A
  4. 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

  1. Step 1: Understand -d option in mktemp

    The -d option tells mktemp to create a temporary directory instead of a file.
  2. Step 2: Correct command substitution syntax

    Use $(mktemp -d) to execute the command and assign the directory path to tempdir.
  3. Final Answer:

    tempdir=$(mktemp -d) -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard