If this operation requires me to create a raw transaction, here's a working example assuming you have UTXOs:
UTXO 1: "txid1", Index 0, Amount 0.01 BTC
UTXO 2: "txid2", Index 1, Amount 0.02 BTC
Destination: "your_destination_address"
# Calculate transaction fee estimate (adjust the fee rate as needed)
TRANSACTION_FEE=$(bitcoin-cli estimatesmartfee 6 "CONSERVATIVE")
# Calculate total input amount from UTXOs
TOTAL_INPUT=0.03
# Calculate the desired output amount
OUTPUT_AMOUNT=0.02
# Calculate the change amount
CHANGE_AMOUNT=$(echo "$TOTAL_INPUT - $OUTPUT_AMOUNT - $TRANSACTION_FEE" | bc)
# Create the raw transaction with change output
RAW_TRANSACTION_HEX=$(bitcoin-cli createrawtransaction '[
{"txid":"txid1", "vout":0},
{"txid":"txid2", "vout":1}
]' '{
"your_destination_address": '$OUTPUT_AMOUNT',
"your_change_address": '$CHANGE_AMOUNT'
}')
# Sign the raw transaction
SIGNED_TRANSACTION_HEX=$(bitcoin-cli signrawtransactionwithwallet "$RAW_TRANSACTION_HEX")
# Broadcast the transaction
bitcoin-cli sendrawtransaction "$SIGNED_TRANSACTION_HEX"
Please correct me if I was wrong.