Introduction
Temporary files let you store data safely for a short time while your script runs. They help avoid conflicts and keep your system clean.
mktemp [OPTION]... [TEMPLATE]
tempfile=$(mktemp) echo $tempfile
tempfile=$(mktemp tmp.XXXXXX) echo $tempfile
tempdir=$(mktemp -d) echo $tempdir
#!/bin/bash # Create a temporary file tempfile=$(mktemp) # Write some text to it echo "Hello, temporary file!" > "$tempfile" # Show the file path and contents echo "Temporary file created at: $tempfile" echo "Contents:" cat "$tempfile" # Clean up by removing the temporary file rm "$tempfile" echo "Temporary file removed."
mktemp command in bash scripting?mktempmktemp is designed to create temporary files or directories with unique names to avoid conflicts.mktemp.mktemp = unique temp file creation [OK]mktemp in bash?mktemp usagemktemp alone creates a temporary file with a unique name.-d creates a temporary directory, not a file. Options --remove and -r are invalid or not standard.mktemp = create temp file [OK]tempfile=$(mktemp) echo $tempfile rm $tempfile
mktemp usagemktemp creates a unique temporary file and returns its path, stored in tempfile.echo $tempfile prints the temporary filename. rm $tempfile deletes the file to keep the system clean.tempfile=mktemp echo $tempfile rm $tempfile
tempfile=mktemp sets the string 'mktemp', not the command output.tempfile=$(mktemp) to run the command and assign its output.tempdir?-d option in mktemp-d option tells mktemp to create a temporary directory instead of a file.$(mktemp -d) to execute the command and assign the directory path to tempdir.Pass the test and this pattern moves unknown